How to set radio button checked from another activity? - java

I have two activities. First activity has different layouts which opens another activity. In another activity I have a segmented group of radio button which I want to set it checked on the basis of the layout is clicked in first activity. To check the radio button I have used this.
segmented5.check(R.id.pickupbakButton);
This sets the button checked but I want it to be checked from another activity on basis of that I have another changes in the layout..
I have first activity like this :
and lets say onClick of PICKUPBOX/BLIND VAN
It should show radio button of pickupbox/blind van checked.
How can I achieve this??
GoBoxActivity
public class GoBoxActivity extends AppCompatActivity {
private Intent i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_go_box);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("GO-BOX");
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
final GoBoxPickupActivity gobox = new GoBoxPickupActivity();
LinearLayout pickupBak = (LinearLayout)findViewById(R.id.PickupBak);
LinearLayout picupBox = (LinearLayout)findViewById(R.id.pickupbox);
LinearLayout engkelBak = (LinearLayout)findViewById(R.id.engkelBak);
LinearLayout engkelBox = (LinearLayout)findViewById(R.id.engkelBox);
pickupBak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
startActivity(i);
}
});
picupBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
startActivity(i);
}
});
engkelBak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
startActivity(i);
}
});
engkelBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
startActivity(i);
}
});
}
}
PickupBoxActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_go_box_pickup);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
segmented5 = (SegmentedGroup) findViewById(R.id.segmented3);
segmented5.setOnCheckedChangeListener(this);
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.pickupbakButton:
engkelTextview.setVisibility(View.GONE);
extraEngkelTextView.setVisibility(View.GONE);
airConditioning.setVisibility(View.GONE);
picBoxTextview.setVisibility(View.VISIBLE);
extraPickBoxTextview.setVisibility(View.VISIBLE);
return;
case R.id.pickupboxButton:
engkelTextview.setVisibility(View.GONE);
extraEngkelTextView.setVisibility(View.GONE);
airConditioning.setVisibility(View.VISIBLE);
picBoxTextview.setVisibility(View.VISIBLE);
extraPickBoxTextview.setVisibility(View.VISIBLE);
return;
case R.id.engkelbakButton:
picBoxTextview.setVisibility(View.GONE);
extraPickBoxTextview.setVisibility(View.GONE);
engkelTextview.setVisibility(View.VISIBLE);
extraEngkelTextView.setVisibility(View.VISIBLE);
airConditioning.setVisibility(View.INVISIBLE);
return;
case R.id.engkelboxButton:
picBoxTextview.setVisibility(View.GONE);
extraPickBoxTextview.setVisibility(View.GONE);
engkelTextview.setVisibility(View.VISIBLE);
extraEngkelTextView.setVisibility(View.VISIBLE);
airConditioning.setVisibility(View.VISIBLE);
return;
default:
engkelTextview.setVisibility(View.GONE);
extraEngkelTextView.setVisibility(View.GONE);
airConditioning.setVisibility(View.GONE);
picBoxTextview.setVisibility(View.VISIBLE);
extraPickBoxTextview.setVisibility(View.VISIBLE);
}
}
}

Modify your code inside onClick in GoBoxActivity, as follows
i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
i.putExtra("selected", 1);
startActivity(i);
And in PickupBoxActivity , do something like this
int selected = getIntent().getIntExtra("selected",1);
after that
switch(selected)
{
case 1:
segmented5.check(R.id.pickupbakButton);
break;
case 2:
.... So on
}
Let me know further for any issues..

Related

OnClickListener for Multiple ImageButton

I'm super new in Java! Pardon for this question although there are similar ones but I'm completely clueless on fixing my problem.
I'm trying to set an OnClickListener for each ImageButton to open a new activity.
The first ImageButton works but not for the subsequent ones, it is unclickable in AVD.
Would greatly appreciate some help on it!
public class CharacterSelect extends AppCompatActivity {
ImageButton arrowbtnright;
ImageButton contchibtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_select);
arrowbtnright = (ImageButton) findViewById(R.id.arrowbtnright);
contchibtn = (ImageButton) findViewById(R.id.contchibtn);
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});
}
});
}
You are currently adding the listener of the second button, from inside the listener of the first button, this is probably not what you want to do .
Try putting both at the same level, i.e :
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
}
});
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});

button onClickListener not working in Android to play a sound

Im trying to set my button to play a song when clicked, but nothing happens, no errors and no force quitting. When i click the button in the rendering, it just presses down.
public class Shows extends AppCompatActivity {
RadioButton radRent1, radRent2, radWicked, radHamilton, radPhantom;
Button btnRent, btnWicked, btnHamilton, btnPhantom;
MediaPlayer mpRent1,mpRent2, mpWicked, mpHamilton, mpPhantom;
int playing;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shows);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(Shows.this, ItemListActivity.class));
}
});
playing = 0;
mpRent1 = new MediaPlayer();
mpRent1 = MediaPlayer.create(this, R.raw.seasons_of_love);
mpRent2 = new MediaPlayer();
mpRent2 = MediaPlayer.create(this, R.raw.seasons_of_love);
btnRent = (Button)findViewById(R.id.btnRent);
btnRent.setOnClickListener(bRent);
}
Button.OnClickListener bRent = new Button.OnClickListener() {
#Override
public void onClick(View v) {
switch(playing){
case 0:
mpRent1.start();
playing = 1;
btnRent.setText("Pause Selected Song");
break;
case 1:
mpRent1.pause();
playing = 0;
btnRent.setText("Play Selected Song");
break;
}
}
};
}
Can anyone see where the error is?
You need to switch on the View's ID
or
Use the set onClickListener on bRent
`
bRent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
//put your logic here
}`
set action in this way
playbutton.setOnClickListener(this);
pausebutton.setOnClickListener(this);
then implement thid
implements View.OnClickListener
than Override
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.play:
//Play voicefile
break;
case R.id.pause:
//Stop MediaPlayer
break;
}
}

android button onClick don't do anything

I'm trying to implement onClick function for a Button on a custom Dialog but nothing happen when i click on the Button .it's the butEditAdd
MainActivity.java :
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final AddDialog addDialog = new AddDialog(MainActivity.this);
addDialog.setContentView(R.layout.dialogedit);
final Button butEditAdd =(Button)addDialog.findViewById(R.id.buttonAdd);
//final Button butEditAdd=addDialog.getAdd();
addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//addDialog.dismiss();
Toast.makeText(getApplicationContext(),"It's working",Toast.LENGTH_SHORT).show();
}
});
addDialog.show();
}
});
addDialog.java:
public class AddDialog extends Dialog implements android.view.View.OnClickListener{
Activity a;
Dialog d;
Button add , cancel;
RadioButton owes,lent ,money,things ;
EditText name ,amount,object,note;
Spinner spin;
public AddDialog(Activity c) {
super(c);
this.a = c;
}
#Override
public void onClick(View v){
};
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dialogedit);
add=(Button)findViewById(R.id.buttonAdd);
cancel=(Button)findViewById(R.id.buttonCancel);
owes = (RadioButton)findViewById(R.id.radioButtonOwes);
lent = (RadioButton)findViewById(R.id.radioButtonLent);
money = (RadioButton)findViewById(R.id.radioButtonAmount);
things =(RadioButton)findViewById(R.id.radioButtonThings);
name = (EditText)findViewById(R.id.editName);
object = (EditText)findViewById(R.id.editTextWhat);
amount =(EditText)findViewById(R.id.editTextAmount);
note =(EditText)findViewById(R.id.editTextNote);
spin=(Spinner) findViewById(R.id.spinnerdevise);
owes.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
lent.setChecked(false);
}
});
lent.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
owes.setChecked(false);
}
});
money.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
things.setChecked(false);
object.setEnabled(false);
amount.setEnabled(true);
spin.setEnabled(true);
object.setText("");
}
});
things.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
money.setChecked(false);
object.setEnabled(true);
amount.setEnabled(false);
spin.setEnabled(false);
amount.setText("");
}
});
object.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
things.performClick();
things.setChecked(true);
}
});
amount.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
money.performClick();
money.setChecked(true);
}
});
thanks for help!
Dialog fragment has separate set of life cycles.If you want to create a class adddialog,then extend dialog fragment and in oncreate of adddialog use setcontent view.
If you want to create simple dialog try this line of code.This will solve your problem.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog addDialog = new Dialog(this);
addDialog.setContentView(R.layout.dialogedit);
final Button butEditAdd =(Button)addDialog.findViewById(R.id.btn);
//final Button butEditAdd=addDialog.getAdd();
addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//addDialog.dismiss();
Toast.makeText(getApplicationContext(),"It's working", Toast.LENGTH_SHORT).show();
}
});
addDialog.show();
}
});
Change AddDialog to Dialog
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog addDialog = new Dialog(MainActivity.this);
addDialog.setContentView(R.layout.dialogedit);
final Button butEditAdd =(Button)addDialog.findViewById(R.id.buttonAdd);
addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(true);
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//addDialog.dismiss();
Toast.makeText(getApplicationContext(),"It's working",Toast.LENGTH_SHORT).show();
}
});
addDialog.show();
}
});
you need it like this
final Button butEditAdd =(Button)addDialog.findViewById(R.id.buttonAdd);
You should inflate the layout first, then find the view by its id.
LayoutInflater inflater = LayoutInflater.from(this);
View dialogView = inflater.inflate(R.layout.dialogedit, null);
final Button butEditAdd =(Button) dialogView.findViewById(R.id.buttonAdd);
.............
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do something
}
});
This should work.
use this instead of getApplicationContext().
Remove the setContentView(R.layout.dialogedit) from your dialog onCreate().
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
And your rest of the code working fine.

Adding second onClick method in Android Studio?

I've just started to learn Java and I've been stumped on adding the code for a second button in an activity. I apologize for my (possible dumb question) and any wrong terminology.
Here is the MainActivity Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, GoActivity.class));
}
});
}
}
How can I add to this code for btnEscape to go to EscapeActivity?
You just have to do exacly the same thing you do with btnGo - this is find your button by id, and then set clickListener to it. It could looks like that:
Button btnEscape = (Button) findViewById(R.id.btnEscape);
btnEscape.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, EscapeActivity.class));
}
});
The optimised way to do that is implement your class with View.OnClickListener and override the method onClick and inside it use switch cases to switch between views and apply on clicks like this:
public class SampleActivity extends AppCompatActivity implements View.OnClickListener{
Button btnGo,btnEscape;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGo = (Button) findViewById(R.id.btnGo);
btnEscape= (Button) findViewById(R.id.btnEscape);
btnGo.setOnClickListener(this);
btnEscape.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnGo:
startActivity(new Intent(MainActivity.this, GoActivity.class));
break;
case R.id.btnEscape:
startActivity(new Intent(MainActivity.this, EscapeActivity.class));
break;
default:
break;
}
}
}
In code, what the two comments by Shiram and Nik above are saying is to add the block below that begins Button btnEscape... after Button btnGo.setOnclick's block.
#Override
protected void onCreate(Bundle savedInstanceState) {
...
Button btnGo = (Button) findViewById(R.id.btnGo);
...
Button btnEscape = (Button) findViewById(R.id.<<name of button in xml>>);
btnEscape.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
... whatever ... }
});
}
In short, the following is a really good pattern to have at your fingertips:
Button btn___ = (Button) findViewById(R.id.<<name of button in xml>>);
btn___.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
... whatever ... }
});

Better Way to Declare List of TextViews?

I have a list of Clickable TextViews that are relatively doing the same thing. You click on it and it goes to that activity. Settings goes to the settings activity. About to the about and so forth. Is there an easier way to declare and set up these clickable TextViews besides this repetitious code?
TextView create,
edit,
settings,
about;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
create = (TextView) findViewById(R.id.create);
create.setTextColor(Color.parseColor("#000000"));
edit = (TextView) findViewById(R.id.edit);
edit.setTextColor(Color.parseColor("#000000"));
settings = (TextView) findViewById(R.id.settings);
settings.setTextColor(Color.parseColor("#000000"));
about = (TextView) findViewById(R.id.about);
about.setTextColor(Color.parseColor("#000000"));
create.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
});
edit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
settings.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
about.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
If you have a small set of items you can take following approach:
public class MyActivity extends Activity implements AdapterView.OnItemClickListener {
private ArrayAdapter<Item> mAdapter;
private static enum Item {Create,Edit,Settings,About}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//-- can set up from external layout also--
ListView list = new ListView(this);
setContentView(list);
list.setOnItemClickListener(this);
mAdapter = new ArrayAdapter<Item>(this,android.R.layout.simple_list_item_1,Item.values());
list.setAdapter(mAdapter);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch (mAdapter.getItem(i)){
case Create:
//--do stuff--
break;
case Edit:
//--do stuff--
break;
case Settings:
//--do stuff--
break;
case About:
//--do stuff--
break;
}
}
}
You can implement the View.OnClickListener .
public class YOURACTIVITY extends Activity implements OnClickListener
And in the onCreate()-
yourTextview.setOnClickListener(this);
and then
#Override
public void onClick(View v) {
// Based on the view , set the action
}

Categories