hii i want to create a ui in which as i select a radio button there should come a textview.
when that button is not selected text view should not be visible. and as button got selected it should be visible..can i implement this??
Inside the listener where you check if the radio button is selected or not:
findViewById(R.id.yourtextview).setVisibility(View.INVISIBLE);
and
findViewById(R.id.yourtextview).setVisibility(View.VISIBLE);
You can choose between INVISIBLE and GONE.
Tutorial to manage radio buttons
Here is sample codes for you...
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
private RadioButton radioButton1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioButton1 = (RadioButton) findViewById(R.id.RadioButton1);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(radioButton1.isChecked()) {
findViewById(R.id.textView).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.textView).setVisibility(View.GONE);
}
}
});
}
}
And here is the xml layout: Main.xml
Hope it will help you a lot...
public class _alefon_radio extends Activity implements OnCheckedChangeListener {
/** Called when the activity is first created. */
private TextView tx;
private RadioGroup rg;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tx = (TextView) findViewById(R.id.tvv);
rg = (RadioGroup) findViewById(R.id.rgroup);
rg.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
// for R.id.option1
case R.id.option1:
tx.setText("option one is checked");
//tx.setVisibility(0); //visible
break;
default:
tx.setText("");
//tx.setVisibility(4); //invisible
}
}
}
and layout:
<?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:id="#+id/tvv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<RadioGroup
android:id="#+id/rgroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioButton
android:id="#+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
</RadioGroup>
</LinearLayout>
I used edit text instead of setting visbile/invisible but also I included visibilty controls (commented) if you`d like to use this way.
I hope this is what you are looking for.
good luck,
Related
I have a button , I want when I click it goes to the page fragment The radiobutton is checked , If, for example, it was a hello , The word hello is printed on the textview in page MainActivity
page fragment
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.java.frag.BlankFragment">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:text="nice to meet you"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton"
android:layout_weight="1" />
<RadioButton
android:text="welcome"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton2"
android:layout_weight="1" />
<RadioButton
android:text="hello"
android:textStyle="bold"
android:layout_gravity="center"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton3"
android:layout_weight="1" />
</RadioGroup>
</LinearLayout>
page MainActivity
package com.example.java.frag;
import android.app.FragmentTransaction;
import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView x = (TextView)findViewById(R.id.textView);
}
public void changetext(View view) {
FragmentManager frag = getFragmentManager();
FragmentTransaction tran = frag.beginTransaction();
BlankFragment s = new BlankFragment();
tran.replace(R.id.con,s);
tran.commit();
}
}
How do I do it?
I hope to get an example
First of all, you should be very clear and precise about your query. Next, you should use EventBus here. It really comes handy when callbacks between different components are needed. To use EventBus, you should add following dependency in the app's build.gradle:
compile 'org.greenrobot:eventbus:3.0.0'
Then, you can create a simple POJO class to refer a callback or event. In this case, you can create a class like this:
class OptionItemEvent {
private String option;
public OptionItemEvent(String option){
this.option = option;
}
public String getOption(){
return option;
}
}
In your BlankFragment.java, you've to make a call to the event in the appropriate listener method, for example in this case:
#Override
public void onCheckedChanged(RadioGroup radioGroup, int radioButtonId) {
RadioButton radioButton = (RadioButton)view.findViewById(radioButtonId);
EventBus.getDefault().post(new OptionItemEvent(radioButton.getText().toString()));
}
In your MainActivity.java, add the following code:
#Override
protected void onResume(){
super.onResume();
EventBus.getDefault().register(this);
}
#Override
protected void onPause(){
EventBus.getDefault().unregister(this);
super.onPause();
}
#Subscribe (threadMode = ThreadMode.MAIN)
public void onOptionItemSelected(OptionItemEvent event){
//Set the value for your TextView
x.setText(event.getOption());
}
I hope this solves your problem.
I made an android application in which the background color of the activity is changing with the click on the RadioButtons in the Activity.
I'm elaborating my question with help of images :
On opening the app :
So we have got 3 RadioButtons on click of any of the RadioButtons the background color of the activity should change to the respective color.
=> when I click on any of the radio button first time then the background color is reflected properly. eg:
After clicking on Green the background color changes to green.
=> But after that whenever I click on any of the button the background color doesn't reflect instantly.
eg:
I clicked on Blue but background color is still Green.
=> Now when I click on other radio buttons then the background color of the previous clicked radio buttons gets reflected in the Activity.
eg:
After I click on red, then I got the background color as blue.
So, how can I fix this issue. I want the background color to be changed immediately after the radio button is checked.
The MainActivity.java file is :
package com.aupadhyay.assignment2;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
RadioGroup radioGroup;
RadioButton redRadioButton, greenRadioButton, blueRadioButton;
LinearLayout linearLayout;
public void initLinearLayout()
{
linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
}
public void initRadioGroup()
{
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
redRadioButton = (RadioButton) findViewById(R.id.redRadioButton);
greenRadioButton = (RadioButton) findViewById(R.id.greenRadioButton);
blueRadioButton = (RadioButton) findViewById(R.id.blueRadioButton);
redRadioButton.setOnCheckedChangeListener(this);
greenRadioButton.setOnCheckedChangeListener(this);
blueRadioButton.setOnCheckedChangeListener(this);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initRadioGroup();
initLinearLayout();
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId())
{
case R.id.redRadioButton:
linearLayout.setBackgroundColor(Color.RED);
break;
case R.id.greenRadioButton:
linearLayout.setBackgroundColor(Color.GREEN);
break;
case R.id.blueRadioButton:
linearLayout.setBackgroundColor(Color.BLUE);
break;
}
}
}
The activity_main.xml file is :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout1"
android:padding="10dp"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/redRadioButton"
android:text="Red"
android:layout_weight="1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/greenRadioButton"
android:text="Green"
android:layout_weight="1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/blueRadioButton"
android:text="Blue"
android:layout_weight="1"/>
</RadioGroup>
</LinearLayout>
try to use setOnClickListener instead of setOnCheckedChangeListener
redRadioButton.setOnClickListener(this);
greenRadioButton.setOnClickListener(this);
blueRadioButton.setOnClickListener(this);
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.redRadioButton:
linearLayout.setBackgroundColor(Color.RED);
break;
case R.id.greenRadioButton:
linearLayout.setBackgroundColor(Color.GREEN);
break;
case R.id.blueRadioButton:
linearLayout.setBackgroundColor(Color.BLUE);
break;
}
}
Here I am trying to make an app where I have 4 options on my MainActivity Screen, User should able to Click on submit only when one of the radio button is selected.(OTHERWISE NOT), which help me to avoid null pointer Exception while submitting the option.
============================= MainActivity.java====================
package com.Xapp.radiobuttondemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
RadioGroup radioGroup;
Button submit;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit=(Button) findViewById(R.id.submit);
radioGroup=(RadioGroup) findViewById(R.id.radioOptions);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
boolean is=group.isActivated(); //This must be something else,Please suggest
if(is==true)
{
submit.setEnabled(true);
}
else
{
submit.setEnabled(false);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
==============================activity_main.xml============================
<LinearLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RadioGroup
android:id="#+id/radioOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/option1" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/option2" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/option3" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/option4" />
</RadioGroup>
<Button
android:id="#+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Submit" />
</LinearLayout>
=======================================strings.xml=================================
<string name="app_name">RadioButtonDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Select One</string>
<string name="option1">Red</string>
<string name="option2">Blue</string>
<string name="option3">Green</string>
<string name="option4">Pink</string>
try this
if (checkedId == R.id.radioButton1) {
//do something
} else if (checkedId == R.id.radioButton2) {
//do something
Actually I think, that if you receive a OnCheckChanged event, is because one of the radiobutton was selected.
OnCheckChanged -> "Called when the checked radio button has changed."
So there is no need to check, because one of then was already selected.
You don't need to do this:
if(is==true)
{
submit.setEnabled(true);
}
else
{
submit.setEnabled(false);
}
Because on a radiogroup once you select one of then, then always there will be one selected.
First of all you are writing Button ENABLE OR DISABLE code inside CheckChanged Event of Radiogroup which will never call if any radio button is not selected.
Updated code:::::
try the below code for what you want.
// get selected radio button from radioGroup
int selectedId = radioOptions.getCheckedRadioButtonId();
if(selectedId == null || selectedId < 1)
{
submit.setEnabled(false);
}
else
{
submit.setEnabled(true);
}
Hope this helps you.
i try make spinner and button with clickHandler
this is my nyoba.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class nyoba extends Activity {
String[] nama_hari = {"Senin", "Selasa", "Rabu","Kamis","Jum'at","Sabtu","Minggu"};
/** Called when the activity is first created. */
Spinner spinner1;
Button btnSubmit;
TextView textView1;
String tes;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSubmit = (Button)findViewById(R.id.btnSubmit);
textView1 = (TextView)findViewById(R.id.textView1);
spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, nama_hari);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(aa);
}
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
tes = nama_hari[arg2];
}
public void clickHandler(View view){
switch (view.getId()){
case R.id.btnSubmit:
textView1.setText(tes);
break;
}
}
}
and this is my main.xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffffff">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickHandler"
android:text="Submit" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
but when i run it to emulator,and i choose 1 option in spinner and click the button , the TextView doesn't show up the word from spinner which i choose
(for example, i choose "senin" at spinner, and click the button, but the text "senin" doesn't appear in the TextView )
i check DDMS and there are no error.....
is the code wrong?
can you help me please which one is wrong?
Thank anyway....
I think you don't set the selection listener on your spinner.
But actually you can just retrieve the spinner's selected position with getSelectedItemPosition in the click handler of your button:
Try this:
public void clickHandler(View view) {
switch (view.getId()) {
case R.id.btnSubmit:
textView1.setText(nama_hari[spinner1.getSelectedItemPosition()]);
break;
}
}
I've created this simple example to illustrate what i am trying to accomplish.
This is my first layout:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class LifeCycleActivity extends Activity implements OnCheckedChangeListener {
/** Called when the activity is first created. */
private RadioButton rbR, rbG, rbB;
private RadioGroup rg;
private Button next;
int color=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rbR = (RadioButton) findViewById(R.id.radio0);
rbB = (RadioButton) findViewById(R.id.radio1);
rbG = (RadioButton) findViewById(R.id.radio2);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
next = (Button) findViewById(R.id.button1);
final Intent it = new Intent(this, next.class);
final Bundle b = new Bundle();
rg.setOnCheckedChangeListener(this);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
b.putInt("color", color);
it.putExtras(b);
startActivity(it);
}
});
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId==rbR.getId()) color=1;
if (checkedId==rbB.getId()) color=2;
if (checkedId==rbG.getId()) color=3;
}
}
This is the second layout:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.*;
public class next extends Activity {
private LinearLayout ll;
private ImageView im;
private TextView tv;
private Button save;
private Bundle extras;
private int color=0;
private String selColor="";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
ll = (LinearLayout) findViewById(R.id.mainll);
ll.setOrientation(LinearLayout.VERTICAL);
im = new ImageView(this);
tv = new TextView(this);
save = new Button(this);
save.setText("save");
extras = getIntent().getExtras();
color = extras.getInt("color");
im.setImageResource(R.drawable.ic_launcher);
ll.addView(im);
if (color == 1) selColor = "RED";
if (color == 2) selColor = "BLUE";
if (color == 3) selColor = "GREEN";
tv.setText(selColor);
tv.setGravity(Gravity.CENTER);
ll.addView(tv);
ll.addView(save);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// here i want to save and exit
// so i can call onPause(), then finish()
// do not know how exactly since i have to follow some goals
// that i need for this example
}
});
}
}
I also have main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="red" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blue" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="green" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>
AND next.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
As you can see, based on color selection in first layout I dynamically build the second layout.
This is what I want to do, help me please (add code if possible):
on the second layout if I press save, I want to save app state (whatever I built on that page) and exit.
If I add one more button to create other dynamic objects on that layout (EditText with name, or another layout with different objects) than if user saves, he/she will also save new objects (This part can be done later)
if user saved that page once then i want him to be able to get the same app state that he/she was in before exiting (of course if he/she selects the same radio button).
If he/she selects another button, i do not need to show that previous state, since the selection is different.
The trick with sharedpreferences did for me. I was finally able to same and retrieve my data onPause and onStart. Even though this worked fine, I appreciate the answer of Kevin_Dingo which is defiantly more professional way to go.