it's my EditText.
I run the app don't type anthing
when I sout gettext().toString
I got really nothing
not null or "" why?
<EditText
android:id="#+id/ed_account"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/bg_edittext"
android:hint="#string/employeeHint1"
android:inputType="number"
android:text=""
android:paddingLeft="10dp" android:paddingStart="10dp"
/>
this is for someone to type some info for me to do something
I use hint, I don't want type anything to over the hint
and I have to make sure user to type something.
Try to modify this line
android:text="some text...."
your question is not clear, am sorry but please improve your english. And without the java code, am not sure i got your problem right, but try using this to check if the EditText is null:
EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
return;
}
Related
I've used the Fullscreen Activity to create a project in Android Studio 2020.3.1. On buld of the app, I get "Unresolved reference: LastChange". The 3 lines pointed to (in FullscreenActivity.kt) are:
override fun onConfigurationChanged(newConfig: Configuration) {
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
LastChange.setText("Landscape")}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
LastChange.setText("Portrait")}
else {
LastChange.setText("Unrecognized")}
super.onConfigurationChanged(newConfig)
}
The TextView (in activity_xml) is coded as:
<TextView
android:id="#+id/LastChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/LastChangeText"
android:textSize="24sp" />
LastChangeText (in strings.xml) is defined as:
<string name="LastChangeText">N/A</string>
Although your textView has "LastChange" id, that's not the way you call it from activity.
At "onCreate" method, after super.onCreate(), you'll need:
setContentView(R.layout.<YOUR_LAYOUT_NAME>);
val lastChangeTextView = (TextView) findViewById(R.id.LastChange);
And, after that, you can call methods on "lastChangeTextView".
lastChangeTextView.setText("Landscape")
Strongly recommend taking a look at Documentation:
https://developer.android.com/guide/topics/ui/declaring-layout
what I am trying to do is show a custom card in a recyclerview.
I can see it on the design view on android studio, but no when the emulator runs.
I just need to display de listItem.
Is is posible? if so, any help or suggestion would be great, thanks.
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcvPoolVariables"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="22dp"
android:layout_marginStart="17dp"
android:layout_marginEnd="17dp"
tools:listitem="#layout/row_pool_objectives"
app:layout_constraintTop_toBottomOf="#+id/containerVariableAndWeeks"
app:layout_constraintBottom_toBottomOf="parent"/>
1.xml
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/categoryview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
tools:listitem="#layout/innercategorylist">
</androidx.recyclerview.widget.RecyclerView>
2.java file
CategoryModel[] myListData1 = new CategoryModel[]{
new CategoryModel( R.drawable.kiwi,"kiwi"),
new CategoryModel(R.drawable.watermelon,"watermelon" ),
new CategoryModel(R.drawable.strawberry,"strawberry")
};
innerCategoryAdapter = new InnerCategoryAdapter(myListData1, new InnerCategoryAdapter.HomeAdapterInterface() {
#Override
public void onRowClick(int searchText) {
}
});
3.Adapter class id define and assign and model class to variable getter setter set
If the "preliminary version" is shown in the design tab, then there are no errors on the part of xml documents. It's in "regular" code part. I think the error lies in the output of elements on the device - either you do not pass data objects for output in recycler view, or there is an error in the processing of this data. Check the recycler view classes architecture and code of an adapter class. Also read this guide, I hope it'll help you.
I'm working on an app where on a button click, the app will take the user from main activity to new activity. I've got everything coded and I am just trying to link up the activities now but I'm getting fatal errors which I cannot really understand. The app crashes when I try the button in testing and in Debug I get the error which I have pasted below. I've also pasted my code for the button and the XML for main activity. Can anybody help me make some sense of this error please? Also, if I have left any information out please let me know. Thanks.
Note: In the code for my button, the button I get the error with is the newactivity one.
Error:
2021-06-15 21:15:39.489 8340-8340/com.project.playshow E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.playshow, PID: 8340
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.playshow/com.fproject.playshow.newactivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.project.playshow.newctivity.onCreate(newactivity.kt:97)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Code used in MainActivity for Button (newactivityopen is the one I'm having issues with)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var dialogbutton = findViewById<Button>(R.id.dialogbtn)
dialogbutton.setOnClickListener{
openDialogue()
}
var newactivityopen = findViewById<Button>(R.id.button_newactivity)
newactivityopen.setOnClickListener{
var intent = Intent(this, newactivity::class.java)
startActivity(intent);
}
}
XML File
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/dialogbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"/>
<Button
android:id="#+id/button_newactivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="569dp"
android:text="NewActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I´m not familiar with Kotlin (only java), but is it possible that you have setted the onClickListener to a Button, which doesn´t exist?
Try replacing these code snippets:
sdkButton.setOnClickListener with dialogbutton.setOnClickListener
button_newactivity.setOnClickListener with newactivityopen.setOnClickListener
I want to display banner(images moving in x direction) in my application. For that i am using ViewFlipper with Translation animation. Please find my below code..
my layout: banner.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ViewFlipper
android:id="#+id/banner_image"
android:layout_width="match_parent"
android:layout_height="258dp" />
</RelativeLayout>
In animation: in_from_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="3000"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
Out Animation: out_to_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="0%" android:toXDelta="-100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="3000"/>
</set>
My Java Code
public class BannerView extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.banner);
ViewFlipper myViewFlipper = (ViewFlipper) findViewById(R.id.banner_image);
//Setting first image in viewFlipper
ImageView imageView1 = new ImageView(HomeView.this);
imageView1.setImageDrawable(getResources().getDrawable(R.drawable.img1));
myViewFlipper.addView(imageView1);
//Setting second image in viewFlipper
ImageView imageView2 = new ImageView(HomeView.this);
imageView2.setImageDrawable(getResources().getDrawable(R.drawable.img2));
myViewFlipper.addView(imageView2);
myViewFlipper.setAutoStart(true);
//Setting in and out animation
myViewFlipper.setInAnimation(HomeView.this,R.anim.in_from_right);
myViewFlipper.setOutAnimation(HomeView.this,R.anim.out_to_left);
//Starting the view filpper to rotate
myViewFlipper.startFlipping();
}
}
Here my problem is,
When i set the duration for in and out animation as "5000", ViewFilpper's behavior is changing like the below,
Image start to move slowly and ending quickly.
I dont know where i am missing. I want to slow down the speed. Please help me resolve the issue.
Update: I whipped up some sample code and there is a big stutter halfway through as if the 2nd view is forcibly moved to catch up to the 1st view. After much trial and error I found that setting android:flipInterval="5000" in the viewflipper xml properties fixed it. I can only assume that the default flipInterval is shorter than 5000 and hence the issue.
<ViewFlipper
android:id="#+id/viewflipper"
android:layout_width="match_parent"
android:layout_height="258dp"
android:flipInterval="5000" >
If you still find the animation not smooth enough:
Put this in your xml for the translate animation:
android:interpolator="#android:anim/linear_interpolator"
And then
android:startOffset="1000"
to slide_out_to_left so it waits a bit before continuing.
Thanks for your reply. I found the solution for my problem. Please find my answer below.
I have changed my interpolator in xml as below:
android:interpolator="#android:anim/decelerate_interpolator"
And then i used android:flipInterval="5000" in my viewflipper solved my problem.
I am tyring to achieve two alignments in the title of an android app. The title according to my requirement needs to have a 'name' which is left aligned and a 'version number' which is right aligned.
So far what I have tried doing is to test whether the label attribute of the application tag of the AndroidManifest.xml files accepts html, but it does not seem to :(. Also from some of the other similar questions I came to know that the style of the title could be changed using a custom theme. But this does not answer my question:
"Can we have two different alignments (say left and right), or for that case two different themes, to the parts of the application name?"
P.S: A screenshot of the desired application title is attached below.
You can create custom title bar ..
here is sample code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.i_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title);
final TextView titleLeft = (TextView) findViewById(R.id.title_left);
final TextView titleRight = (TextView) findViewById(R.id.title_right);
titleLeft.setText("Checker");
titleRight.setText("Version 5.44");
}
i_title.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/title_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="app name" />
<TextView
android:id="#+id/title_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="app version" />
</RelativeLayout>