How i can compare the value of an id with java - java

I have a simple xml with a question and two buttons. When one of buttons is pushed i will compare if the id of the pushed button is equal to "Blanco" or "Negro".
The XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/pregunta" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Blanco"
android:onClick="respuesta"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Negro"
android:onClick="respuesta"/>
</LinearLayout>
</LinearLayout>
This is de java code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void respuesta(){
//The doubt.
//Here the if/else to compare ID with the button text
}
}

Don't do it like that. Create an onClickListener for each Button, so you know exactly which one is being pressed. For example:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener{
public void onClick(View v) {
// call code here, knowing that button1 was pressed
}
});
button2.setOnClickListener(new View.OnClickListener{
public void onClick(View v) {
// call code here, knowing that button2 was pressed
}
});
}
}

implement onClickListener into your Activity.
public class MainActivity extends ActionBarActivity implements OnClickListener
Declare the Button variables inside your class
Button btblanco, btnegro;
Implement the clickListener event on onCreate
btblanco = (Button) findViewById(R.id.button1);
btnegro = (Button) findViewById(R.id.button2);
btblanco.setOnClickListener(this);
btnegro.setOnClickListener(this);
And put this inside onClickListener method.
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
Toast.makeText(getApplicationContext(), "Blanco", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(getApplicationContext(), "Negro", Toast.LENGTH_SHORT).show();
break;
}}

if you are interested in just knowing which button was clicked, then add a onclicklistener to the button objects. Google for it a bit and the samples you find will show you how to use a switch case structure to d actions based on which button was clicked.

Related

Change Button Color When Clicked Android Studio

How can I change the color of the button when it's clicked, I'm making a quiz app and when the button is clicked it will change its color
You can use a MaterialButtonToggleGroup to select from a group of choices:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="#+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleSelection="true"
app:selectionRequired="true">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
style="?attr/materialButtonOutlinedStyle"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
style="?attr/materialButtonOutlinedStyle"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
style="?attr/materialButtonOutlinedStyle"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
Just set View.OnClickListener to your Button and setBackgroundColor to it.
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundColor(Color.parseColor("#ff0000"));
}
});
If you want to reset color when the other Button is clicked, you can use a common OnClickListener among the Buttons. Below is an example:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button1;
private Button button2;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
button1.setBackgroundColor(Color.parseColor("#ff0000"));
button2.setBackgroundColor(Color.parseColor("#0000ff"));
button3.setBackgroundColor(Color.parseColor("#0000ff"));
break;
case R.id.button2:
button1.setBackgroundColor(Color.parseColor("#0000ff"));
button2.setBackgroundColor(Color.parseColor("#ff0000"));
button3.setBackgroundColor(Color.parseColor("#0000ff"));
break;
case R.id.button3:
button1.setBackgroundColor(Color.parseColor("#0000ff"));
button2.setBackgroundColor(Color.parseColor("#0000ff"));
button3.setBackgroundColor(Color.parseColor("#ff0000"));
break;
}
}
}

How do I pass string from SecondActivity to MainActivity on Android Studio?

I have a public String stringPass in SecondActivity.java that I want to pass to MainActivity.java so that when I click a button, the string from the SecondActivity.java updates a TextView tv that is declared in MainActivity.java.
The bug I'm getting is that when I press the button, the string in the SecondActivity.java is not shown.
Instead, It goes from "Hello World!" to no text displayed.
FYI, i'm going to add strings to MainActivity.java from multiple activities, so I want it this particular way for my organization.
Thanks!
SecondActivity.java
public class SecondActivity extends AppCompatActivity{
public String stringPass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stringPass = "this is from SecondActivity";
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = findViewById(R.id.tv);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(new SecondActivity().stringPass);
}
});
}
}
activity_main.xml
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Is there much content in the second activity? Is it an actual activity with much going on? If not replace it with a dialog box, when the ok button is clicked it can update the text box.

Multiple activity on one button click

I have a problem with creating 2 activities for 1 button. I created a button and I managed to connect that button with one activity but I dont know how to add another activity to the same button.. What am I trying to do is that when we press on that button it will start playing animation (I managed to do that) but I also want it to send SMS to a specific number. I have code for that, but I dont know how to include / connect everything together.
Code from a button:
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
Code from a imageview (animation):
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:scaleType="centerInside"
android:src="#drawable/keers" />
Code from main activity, for animation:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
button = (Button) findViewById(R.id.button);
running = false;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(!running){
((AnimationDrawable) imageView.getDrawable()).start();
running = true;}
}
}
);
}
}
Now what I want to add for the same button is this :
public void sendText(View paramView)
{
Toast.makeText(this, "SENT",Toast.LENGTH_SHORT).show();
SmsManager.getDefault().sendTextMessage("+3564245237",null,"1", null, null);
I'm not that experienced in programming, so it might be pretty easy for you guys.
Both actions seen to be fired by the same Button, so you may add both to the onClickListener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(!running){
((AnimationDrawable) imageView.getDrawable()).start();
running = true;}
Toast.makeText(view.getContext(), "SENT",Toast.LENGTH_SHORT).show(); //Or YourActivity.this
SmsManager.getDefault().sendTextMessage("+3564245237",null,"1", null, null);
}
}

How can i have a button visible after another button is click in android

Hi friends can you please help me how can i make a button visable after another button is clicked
Try this:
buttonONE.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonTWO.setVisibility(View.VISIBLE);
}
});
In layout xml:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonTWO"
android:layout_gravity="center_horizontal"
android:visibility="gone"/>
Like this:
public class YourClass extends Activity{
Button button1;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from your xml
setContentView(R.layout.xml_layout_name);
// Locate the buttons in your xml
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
//set listener on button1
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//make button2 visible when button1 is pressed
button2.setVisibility(View.VISIBLE);
}
});
}
}
In xml_layout_name:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:visibility="gone"/>
</RelativeLayout>

Edittext, on submit need to show on another window in android

In android,i want to get a string from user using EditText and on click of submit button the string need to be show on another page/pane.
here is my fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText android:id="#+id/msg"
android:hint="#string/entertheip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text"
/>
<Button android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button1"
/>
<TextView android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/thetext"
android:visibility="invisible"
/>
</LinearLayout>
and corresponding java class is this
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Button bu = (Button)findViewById(R.id.button1);
bu.setOnClickListener(new View.OnClickListener() {
private TextView tv = (TextView)findViewById(R.id.tv);
#Override
public void onClick(View v) {
// to view the ip in another page
try{
EditText et = (EditText)findViewById(R.id.msg);
String t = et.getText().toString();
tv.setText(t);
}catch(NullPointerException e){
e.printStackTrace();
}
}
});
}
}
But i am getting the Edit Text and Button only, the text is not showing in another page. Kindly help in this.
place this inside onClick
tv.setVisibility(View.VISIBLE);
You will have to use an Intent to go to another activity, like
Intent i=new Intent(MainActivity.this, someClass.class);
startActivity(i);
save the string in app preference and use the preference tag in that class and set the string to some textview you want there ...
try below way:-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Button bu = (Button)findViewById(R.id.button1);
TextView tv = (TextView)findViewById(R.id.tv);
EditText et = (EditText)findViewById(R.id.msg);
bu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// to view the ip in another page
try{
String t = et.getText().toString();
tv.setText(t);
}catch(NullPointerException e){
e.printStackTrace();
}
}
});
}
Remove this line from TextView tag of your XML File:-
android:visibility="invisible"
You can navigate between activities using Intent and also pass data with it
Using Intents to Create Flows
Simple way on your case is
On Button click
Intent i = new Intent(MainActivity.this, ActivityTwo.class);
i.putExtra("username", "foobar");
startActivity(i); // brings up the second activity
On Another Activity
// ActivityTwo.java (subactivity) can access any extras passed in
protected void onCreate(Bundle savedInstanceState) {
String username = getIntent().getStringExtra("username");
}
You have not added onClick to button: Fix that.
<Button android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button1"
android:onClick="onBtnClick"
/>
And you need not override onClick if you are not implementing onClickListener in activity.
public void onBtnClick(View v) {
// to view the ip in another page
try{
EditText et = (EditText)findViewById(R.id.msg);
String t = et.getText().toString();
tv.setText(t);
tv.setVisibility(View.VISIBLE); //as you have kept visibility invisible in xml
}catch(NullPointerException e){
e.printStackTrace();
}
}
Hope it helps.

Categories