Text Fields in android-studio - android

Can someone tell me how to use a Text Field with an onclick listener on a button in android studio and give me the xaml code too.

First you need write in xml file what you need, we need button and textview:
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="30dp"
android:text="textEdit"
android:layout_marginLeft="5dp"
android:textColor="#fff"
android:layout_marginRight="2dp"
android:background="#333"
android:onClick="ButtonOneClick"
android:layout_marginBottom="4dp"
/>
<TextView
android:id="#+id/txtView"
android:layout_width="50dp"
android:layout_height="30dp"
android:hint="text view"
/>
After xml you need define this in class. Because we define android:onClick="ButtonOneClick" in xml,
we can just call it , and set what you want to do.
TextView tv;
tv =(TextView)findViewById(R.id.txtView);
public void ButtonOneClick(View view) {
tv.setText("Test");
}
Other method is to declare button to, and set onClickListener.
Button butt;
butt= (Button) findViewById(R.id.button1);
butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tv.setText("Test")
}
});

in tampil.xml
<Button
android:id="#+id/button1"
android:text="PENCET"
android:onClick="onPencet" />
and in u're class, use public void nameonClick(view v){ statement }
public class Tampil {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onPencet(View v){
**U're statement }
}

Related

Two buttons with same id and same behavior in android

I want to navigate two buttons to the same class. In one XML, I have two buttons, both should work the same way. How can I achieve this?
Define two button with different id and point to same function in onClick event
<Button
android:id="#+id/button1"
android:onClick="executeSameBehavior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:onClick="executeSameBehavior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
And then implement your logic here:
public void executeSameBehavior(View view) {
// Your logic
}
You can simply assign one clickListener interface to both setOnClickListeners
Button a = findViewById(R.id.a);
Button b = findViewById(R.id.b);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Something for both
}
};
a.setOnClickListener(listener);
b.setOnClickListener(listener);

My Button is not responding in android studio

When i run the app. The buttons refuse to respond. I set a Toast and a logcat to check for response. but non. please help resolve
this is my source code
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:id="#+id/button"
tools:onClick="topClick" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:id="#+id/button2"
tools:onClick="bottomClick" />
and for my java methods;
public class MyActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
//Testing the Toast and Log in action
Toast.makeText(this, "Can you see me?", Toast.LENGTH_SHORT).show();
Log.i("info", "Done creating the app");
}
//creating method topclick
public void topClick(View v) {
Toast.makeText(this, "Top button clicked", Toast.LENGTH_SHORT).show();
Log.i("info", "The user clicked the top button");
}
//creating method bottomclick
public void bottomClick(View v) {
Toast.makeText(this, "Bottom button clicked", Toast.LENGTH_SHORT).show();
Log.i("info", "the user clicked the bottom button");
}
}
Replace
tools:onClick
with
android:onClick
for both buttons.
For more context that will help you understand why this change is required, read up on the tools namespace:
Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
I would highly not recommended using that implementation of your as you can hardly know which method for what button. Try this instead.
XML
<Button
android:text="Button"
android:id="#+id/button1" />
<Button
android:text="Button"
android:id="#+id/button2" />
ACTIVITY.JAVA
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Button button01 = (Button)findViewById(R.id.button01);
Button button02 = (Button)findViewById(R.id.button02);
button01 .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "This is button01", Toast.LENGTH_SHORT).show();
}
});
button02 .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "This is button02", Toast.LENGTH_SHORT).show();
}
});
}

Set button clickable property to false

I need to disable the click-event for a button in Android. Just as a sample, I have tried doing the following. I have taken a TextView named it (entered a text) as Name. The condition checks if, TextView is empty button and clickable should be set to false. However this does not happen when the Toast is printed. Can somemone tell me the reason. Also if the text field is not empty I want to reset the clickable event for button as true.
Java File:
public class ButtonclickabkeActivity extends Activity {
TextView tv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Name");
btn = (Button) findViewById(R.id.button1);
if (tv.getText().toString().length() != 0) {
btn.setClickable(false);
Toast.makeText(getApplicationContext(), "" + tv.getText().toString().length(), Toast.LENGTH_LONG).show();
} else {
btn.setClickable(true);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use
btn.setEnable(false);
instead of
btn.setClickable(false);
User 370305 is correct. .setEnable is what your looking for. Or you could use android:clickable in the layout XML file.
In my case
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
view.setEnabled(false);
}
});

What am I missing in the following buttons code?

I am trying to increment and decrement the middle textview via buttons on the sides. The application starts up finely but by the time I click on any of the buttons it gets closed with following error.
Error: process <package> has stopped unexpectedly.
My main.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="250dp"
android:text="+"
android:textSize="40dp" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="80dp"
android:layout_toRightOf="#+id/button1"
android:layout_marginTop="75dp"
android:layout_marginLeft="80dp"
/>
<Button
android:id="#+id/button2"
android:layout_width="50dp"
android:layout_height="250dp"
android:layout_alignParentRight="true"
android:text="-"
android:textSize="40dp" />
My java file:
public class IncrementDecrementActivity extends Activity {
int counter;
Button add, sub;
TextView tv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button) findViewById(R.id.button1);
sub = (Button) findViewById(R.id.button2);
tv = (TextView) findViewById(R.id.tv1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
tv.setText(counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
tv.setText(counter);
}
});
}
}
Well. This should be.
In this statement
counter--;
tv.setText(counter);
It should be
counter--;
tv.setText(String.valueOf(counter));
I guess the error is
ResourceNotFoundException
How you encountered this error?
on the code above.
you declared int counter;
let's consider the value of counter is 0
and then
you called
counter--; // take note this is an integer
tv.setText(counter);
counter is now -1
by calling setText(counter);
will search first a string value from strings.xml with an integer -1 and set the text to textview
If android fails to find that string it will throw ResourceNotFoundException
:)

Make a Buttonistener in a second layout

How would I implement a Buttonlistener for a second layout which is still be called in the main Acitivity?
I already tried it by a named Button listener and via an anonymous. But still get nullpointer Exceptions.
Code:
back = (Button) findViewById(R.id.backToMain);
if(back != null)
back.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
setLayout(R.layout.main);
}
});
Layout.xml
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/backToMain"
android:text="Zurück">
</Button>
Try this
Change the button attr tag
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/backToMain"
android:text="Zurück"
android:onClick="goBack">
</Button>
In your activity create a method
public void goBack(View v) {
//Write code here
}

Categories

Resources