If I have a radiogroup with 7 radiobuttons and I want them to be aligned like that:
button2 button5
button1 button3 button6
button4 button7
Is there a way I can do the above with one RadioGroup (I am ok with even button1 being on first line but this is the general layout I am looking for)
Please help
Thanks
You could implement your own button handling logic, based on this http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html:
public class RadioButtonHandler implements RadioButton.OnCheckedChangeListener {
private CompoundButton currentlyCheckedButton = null;
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked) {
currentlyCheckedButton .setChecked(false);
currentlyCheckedButton = button;
}
}
public void addButton(RadioButton button) {
// if this is the first button, set it as the current button
if (currentlyCheckedButton == null) {
currentlyCheckedButton = button;
}
button.setOnCheckedChangeListener(this);
}
public CompoundButton getCheckedRadioButton() {
return currentlyCheckedButton ;
}
}
Instantiate this class and call addButton for each button that the class should control. Then you can layout your buttons in whatever view group you need. Add extra logic to the onCheckChanged callback or call getCheckedRadioButton which will return a reference to the currently checked button.
[EDIT] Fixed typo = getCheckedButton -> getCheckedRadioButton
Related
I need a way to know which of my buttons was pressed first. The app layout is more or less like this.:
Button1 Button2
Button3 Button4
Button5 Button6
Button7 Button8
Button9 Button10
Button11 Button12
And if one of the buttons of it's "line" is pressed, the other disappears. The thing is, I have no idea how to know which one of all these 12 buttons was pressed first, then pressed second, then pressed third and so on...
The code I have for hiding buttons works well, but that's pretty much the easy part.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
button1.setVisibility(View.GONE);
button2.setVisibility(View.GONE);
}
});
I searched but maybe I don't know what exactly to search for, and I didn't find a good answer.
You can do it like this:
1) Have a HashMap where you link both of your buttons on the same line to each other. 2) Have an ArrayList of button id's where you can hold the order of presses. 3) Implement a method which will perform the mapping and call it in your Activity's #onCreate method. 4) Set your global listener instance to all of your buttons.
private HashMap<Integer, Integer> buttonMap = new HashMap<>();
private ArrayList<Integer> buttonPressedOrder = new ArrayList<>();
// A global listener instance to be set to all of your buttons
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View selectedButton) {
int selectedButtonId = selectedButton.getId();
//Add pressed button to pressed buttons list
buttonPressedOrder.add(selectedButton.getId());
//Find button to hide and hide it
int hidingButtonId = buttonMap.get(selectedButtonId);
Button hidingButton = findViewById(hidingButtonId);
hidingButton.setVisibility(View.GONE);
}
}
//Put these inside your activity#onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mapButtons();
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
...
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
}
// A method for mapping your buttons in the same line to each other
private void mapButtons(){
buttonMap.put(R.id.button1, R.id.button2)
buttonMap.put(R.id.button2, R.id.button1)
buttonMap.put(R.id.button3, R.id.button4)
buttonMap.put(R.id.button4, R.id.button3)
...
}
Whenever you need to see in which order the buttons are pressed, use this method
public void getButtonPressedOrder(){
Resources res = getResources();
int numberOfPressedButtons = buttonPressedOrder.size();
for(int i=0; i<numberOfPressedButtons; i++){
Log.i("PressOrder", res.getResourceEntryName(buttonPressedOrder.get(i))
+ " is pressed at " + (i+1) + " order");
}
}
which will log something like:
I/PressOrder: button1 is pressed at 1 order
I/PressOrder: button5 is pressed at 2 order
I/PressOrder: button10 is pressed at 3 order
Hope this helps!
I have MainText1 displays a text, when you click on button 3 in the Menu MainText1 displays text3 which is coming from the super class Text. What I want is that dynamically when you click on any button it reads the number and displayed the respective text, that's all about. ;)
I want to get rid of switch case in my activity, so I'm trying now for 2days :( to change the name of the string variable dynamically, but I think I'm using a wrong code as string variable is different from resources (confused), here's my code, this really challenging me this weekend:
public class MainText1 extends Text {
String
tx1=text1,tx2=text2,tx3=text3,
tx,stringReceived;//text1,text2...textn strings coming from the Super class Text
num = Integer.parseInt(getIntent().getStringExtra("somekey1")); // this data is coming from the menu, it depends on which button is clicked
tx="text"+num; // text is the name of the string variable, it should be in format like that : text1,text2,...textn which have predefined string content
stringId = getApplicationContext().getResources().getIdentifier(tx, "string", getPackageName());
if (stringId > 0) {
stringReceived=getApplicationContext().getResources().getString(stringId);
I guess what you're trying to do is to change a TextView contents. So, you could do the following:
public YourActivity extends Activity {
//Here you will declare your layout items
private Button button1;
private Button button2;
private Button button3;
private TextView txtView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//Here you will get you layout elements
button1 = (Button) findViewById(R.id.button1_id);
button2 = (Button) findViewById(R.id.button2_id);
button3 = (Button) findViewById(R.id.button3_id);
txtView = (TextView) findViewById(R.id.txtview_id);
//Now you will have to set the onClickListeners
button1.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button1
txtView.setText(getResources()
.getString(R.string.button1_string);)
}
});
button2.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button2
txtView.setText(getResources()
.getString(R.string.button2_string);)
}
});
button3.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button3
txtView.setText(getResources()
.getString(R.string.button3_string);)
}
});
}
}
This should do the trick. Although, like the other guys suggested it, you should take a look at some tutorials before coding on
I have an activity ColorChoiceActivity that displays radio group for the user to select a color preference from. I would like to be able to change the xml layout displayed in my PlayGameActivity based on which of these buttons is selected.
Currently I have the PlayGameActivity just display the default layout:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battlefield_default);
}
My ColorChoiceActivity has the following so far:
public class ColorChoiceActivity extends Activity {
private RadioGroup radioColorGroup;
private RadioButton radioColorButton;
private Button saveColorBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_choice_screen);
addListenerOnButton();
}
public void addListenerOnButton()
{
radioColorGroup = (RadioGroup) findViewById(R.id.radioColor);
saveColorBtn = (Button) findViewById(R.id.saveColorBtn);
saveColorBtn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v){
//get selected radio button from radioGroup
int selectedId = radioColorGroup.getCheckedRadioButtonId();
//find the radiobutton by returned id
radioColorButton = (RadioButton) findViewById(selectedId);
//change battlefield layout based on selected color
}
});
}
}
I am not sure how to go about changing the layout displayed in the PlayGameActivity based on what is chosen in ColorChoice. Currently I have several separate battlefield layout files but I don't know how to switch the content view to them based on user choice. Is there a way to do this or am I approaching the problem in the wrong way?
In onClick() listner, just remember selected battlefield and then, conditionally set layout using
setContentView("your selected layout")
in on create method.
Better option is to use the menu instead of the radiogroup
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.color1:
break;
}
}
I have this problem with my ToggleButton.
I want it to create/delete a button upon being toggled, and at the same time add content/functions to the button, like drawable and such.
This is the current code:
public class BillardScoreboardActivity extends Activity {
/** Called when the activity is first created. */
Button minuskegle, minuskugle, pluskugle, pluskegle, plusmidkegle, minusmidkegle, miss;
ToggleButton toggle;
LinearLayout bottomlayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton) findViewById(R.id.bRedGreen);
toggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
pluskugle = (Button) findViewById(R.id.bBallhole);
minuskugle = (Button) findViewById(R.id.bBallhole);
pluskegle = (Button) findViewById(R.id.bKegle);
minuskegle = (Button) findViewById(R.id.bKegle);
plusmidkegle = (Button) findViewById(R.id.bKeglemid);
minusmidkegle = (Button) findViewById(R.id.bKeglemid);
bottomlayout = (LinearLayout) findViewById(R.id.bottomlayout);
miss = (Button) findViewById(R.id.bMiss);
if(toggle.isChecked())
{
minuskugle.setBackgroundResource(R.drawable.redballinhole);
minuskegle.setBackgroundResource(R.drawable.redkegle);
minusmidkegle.setBackgroundResource(R.drawable.midkegleminus);
miss.setBackgroundResource(R.drawable.missbutton);
miss.setVisibility(View.VISIBLE);
}
else
{
pluskugle.setBackgroundResource(R.drawable.whiteballinhole);
pluskegle.setBackgroundResource(R.drawable.kegleb);
plusmidkegle.setBackgroundResource(R.drawable.midkegleplus);
miss.setVisibility(View.GONE);
}
}
});
}
The current problem is that it can't find the (buttontest) in this part of the code:
else
{
pluskugle.setBackgroundResource(R.drawable.whiteballinhole);
pluskegle.setBackgroundResource(R.drawable.kegleb);
plusmidkegle.setBackgroundResource(R.drawable.midkegleplus);
bottomlayout.removeView(buttontest);
}
And as mentioned earlier, the second problem is to make the button inherit some functions/content.
for bigger version: http://i.imgur.com/KxKvh.png
Btw... Everytime i start up the application, it gives me 2 apps to choose between, whereof only the bottom one works:
I guess the problem is that the togglebutton's initial state is 'checked'. That means when you click it the first time, isChecked() will return false and the else-part of your code will be executed. But at that point, buttontest hasn't been added to bottomlayout yet.
I recommend you to have the button inside the layout by default and call buttontest.setVisibility(View.GONE) when you would like to hide it and buttontest.setVisibility(View.VISIBLE) when it needs to be shown.
As for your second question, just call setBackgroundResource/Drawable to add content (like you're already doing it with the other buttons). If you say you want to add functionality, I assume you intend to do something when the button is clicked? If yes, add a View.OnClickListener.
Hope I could help you.
Is there a way to have 1 onClick Lister for many buttons where I can toss a case statement to do things based on what buttons were clicked.
I know I can make 100 different listeners for 100 buttons but I have to think I can create some nifty variables to do it in less lines of code.
Button btn1, btn2;
public void onCreate(Bundle b)
{
// here you do normal things like assigning a
// content view to the activity, initiate buttons, etc.
// then you assign the same listener to both buttons
btn1.setOnClickListener(yourListener);
btn2.setOnClickListener(yourListener);
}
// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener yourListener = new View.OnClickListener(){
public void onClick (View v){
if( v == btn1 ){
// do something
}
elseif( v == btn1 ){
// do another thing
}
}
};
If you are using 1.6+ version of the SDK you can use android:onClick to set the onClick handler of a view. In your activity you must have a method with the following signature. The view is the view that was clicked.
void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
//do something fantastic;
break;
}
}
public class MainActivity extends Activity implements View.OnClickListener{
btnXXX.setOnClickListener(this);
public void onClick(View v) {
if (v.getId()==R.id.btnXXX){
dialog.show();
} else {
handleOtherViews(v);
}
}
Alternatively, you can specify the method to call in xml:
<Button android:id="#id/button" android:text="#string/button" android:onClick="someMethod" />