Multiple row Radio buttons in android? - android

I am having problem in having radioButtons in multiple Rows
this is my xml
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_one0Id"
android:textSize="13sp"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:text="5%"
android:id="#+id/radio_one5Id"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="10%"
android:textSize="13sp"
android:layout_weight="1"
android:id="#+id/radio_one10Id"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="20%"
android:layout_weight="1"
android:textSize="13sp"
android:onClick="oneRadioButtonClicked"
android:id="#+id/radio_one20Id"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="35%"
android:id="#+id/radio_one35Id"
android:textSize="13sp"
android:onClick="oneRadioButtonClicked"
android:layout_weight="1"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="50%"
android:textSize="13sp"
android:id="#+id/radio_one50Id"
android:onClick="oneRadioButtonClicked"
android:layout_weight="1"
/>
</RadioGroup>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="65%"
android:textSize="13sp"
android:id="#+id/radio_one65Id"
android:onClick="oneRadioButtonClicked"
android:layout_weight="1"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="75%"
android:textSize="13sp"
android:layout_weight="1"
android:id="#+id/radio_one75Id"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="85%"
android:textSize="13sp"
android:id="#+id/radio_one85Id"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:text="95%"
android:id="#+id/radio_one95Id"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="100%"
android:id="#+id/radio_one100Id"
android:textSize="13sp"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
</RadioGroup>
</RadioGroup>
this is code
public void oneRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_one0Id:
if (checked)
one = "0";
break;
case R.id.radio_one5Id:
if (checked)
one = "5";
break;
case R.id.radio_one10Id:
if (checked)
one = "10";
break;
case R.id.radio_one20Id:
if (checked)
one = "20";
break;
case R.id.radio_one35Id:
if (checked)
one = "35";
break;
case R.id.radio_one50Id:
if (checked)
one = "50";
break;
case R.id.radio_one65Id:
if (checked)
one = "65";
break;
case R.id.radio_one75Id:
if (checked)
one = "75";
break;
case R.id.radio_one85Id:
if (checked)
one = "85";
break;
case R.id.radio_one95Id:
if (checked)
one = "95";
break;
case R.id.radio_one100Id:
if (checked)
one = "100";
break;
default:
System.out.println("default");
}
}
this will look like
it will select both the buttons in 2 rows, i want it to select only one button in those rows, thanks for any help

Put one radiogroup with vertical orientation and add two LinearLayouts:
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<RadioButton
android:id="#+id/radio_one0Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one5Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="5%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one10Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="10%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one20Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="20%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one35Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="35%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one50Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="50%"
android:textSize="13sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio_one65Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="65%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one75Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="75%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one85Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="85%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one95Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="95%"
android:textSize="13sp" />
<RadioButton
android:id="#+id/radio_one100Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="100%"
android:textSize="13sp" />
</LinearLayout>
</RadioGroup>

From searching around, there doesn't appear to be a way of doing it,
This means you will have to implement this layout behaviour manually. Two possible options are:
Create a copy of RadioGroup to extend a different layout, or at least allow you control it dynamically.
Implement your own custom layout to replace RadioGroup that extends a layout of your choice, and implements OnClickListener. There's a good example How to group a 3x3 grid of radio buttons?.

I was researching this a lot, and I finally found a solution. If you want to have something like this:
First you need to download/create a new class like this: link, since RadioGroup uses LinearLayout by default. Now you have a RadioGroup that uses RelativeLayout. The only thing left is to separate radio buttons by percentage (just like with weightSum, it's just that you don't have weightSum in RelativeLayout, only LinearLayout) by using a neat little hack:
<rs.cdl.attendance.UI.RelativeRadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<View
android:id="#+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<RadioButton
android:id="#+id/start_radio_button"
android:layout_alignRight="#id/strut"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<RadioButton
android:id="#+id/finish_radio_button"
android:layout_alignLeft="#id/strut"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Finish" />
<RadioButton
android:id="#+id/pause_radio_button"
android:layout_alignRight="#id/strut"
android:layout_alignParentLeft="true"
android:layout_below="#id/start_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause" />
<RadioButton
android:id="#+id/continue_radio_button"
android:layout_alignLeft="#id/strut"
android:layout_alignParentRight="true"
android:layout_below="#id/finish_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue" />
</rs.cdl.attendance.UI.RelativeRadioGroup>

This worked for me.
The first line (NameRadioGroupe2.clearCheck();) clear the other Radiogroup, and the second line add a checkmark in the button that was checked
public void oneRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_one0Id: {
one = "0";
NameRadioGroupe2.clearCheck();
NameRadioGroupe1.check(view.getId());
break;
}
break;
case R.id.radio_one5Id: {
NameRadioGroupe2.clearCheck();
NameRadioGroupe1.check(view.getId());
one = "5";
break;
}
.
.
.
.
.
case R.id.radio_one65Id: {
NameRadioGroupe1.clearCheck();
NameRadioGroupe2.check(view.getId());
one = "65";
break;
}
case R.id.radio_one75Id: {
NameRadioGroupe1.clearCheck();
NameRadioGroupe2.check(view.getId());
one = "75";
break;
}
.
.
.
.
.

I was trying to work out the same thing.
What I ended up doing was adding multiple RadioGroups within their own LinearLayouts. When selecting a radiobutton from the other RadioGroup to the one that is currently selected to ensure that the first RadioGroup buttons were no longer selected I added .Checked = false to the radiobuttons .Click function. And then because I experienced bugs at first where sometimes the newly clicked radio button wouldnt check, I added a .Checked = true to the actual radio button.
My XML
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:padding="10dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
style="#style/radios"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rad1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/radios"
android:text="1"
android:checked="true" />
<RadioButton
android:id="#+id/rad2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/radios"
android:text="2" />
<RadioButton
android:id="#+id/rad3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/radios"
android:text="3" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:padding="10dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
style="#style/radios"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rad4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/radios"
android:text="4" />
<RadioButton
android:id="#+id/rad5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/radios"
android:text="5" />
</RadioGroup>
</LinearLayout>
And then my C#
var radio1 = FindViewById<RadioButton>(Resource.Id.rad1);
var radio2 = FindViewById<RadioButton>(Resource.Id.rad2);
var radio3 = FindViewById<RadioButton>(Resource.Id.rad3);
var radio4 = FindViewById<RadioButton>(Resource.Id.rad4);
var radio5 = FindViewById<RadioButton>(Resource.Id.rad5);
radio1.Click += delegate
{
radio2.Checked = false;
radio3.Checked = false;
radio4.Checked = false;
radio5.Checked = false;
radio1.Checked = true;
};
radio2.Click += delegate
{
radio1.Checked = false;
radio3.Checked = false;
radio4.Checked = false;
radio5.Checked = false;
radio2.Checked = true;
};
radio3.Click += delegate
{
radio1.Checked = false;
radio2.Checked = false;
radio4.Checked = false;
radio5.Checked = false;
radio3.Checked = true;
};
radio4.Click += delegate
{
radio1.Checked = false;
radio2.Checked = false;
radio3.Checked = false;
radio5.Checked = false;
radio4.Checked = true;
};
radio5.Click += delegate
{
radio1.Checked = false;
radio2.Checked = false;
radio3.Checked = false;
radio4.Checked = false;
radio5.Checked = true;
};
Primative, but it worked for me.

One simple way to make multiple lines of radio buttons is to use MultiLineRadioGroup library. It supports as many rows and columns as you like.
The use is simple:
In your project's build.gradle file add:
allprojects {
repositories {
...
maven {
url "https://jitpack.io"
}
...
}
}
In your Application's or Module's build.gradle file add:
dependencies {
...
compile 'com.github.Gavras:MultiLineRadioGroup:v1.0.0.6'
...
}
You can use a string array resource in the xml to create your view like that:
In the layout's XML add:
<com.whygraphics.multilineradiogroup.MultiLineRadioGroup xmlns:multi_line_radio_group="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_activity_multi_line_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
multi_line_radio_group:default_button="button_2"
multi_line_radio_group:max_in_row="3"
multi_line_radio_group:radio_buttons="#array/radio_buttons" />
and in arrays.xml add:
<string-array name="radio_buttons">
<item>button_1</item>
<item>button_2</item>
<item>button_3</item>
<item>button_4</item>
<item>button_5</item>
</string-array>
or add them programmatically:
mMultiLineRadioGroup.addButtons("button to add 1", "button to add 2", "button to add 3");

Related

How to show Radio Button Checked in Radio Group when value get from Web Service?

I have five BadioButton in RadioGroup. Now what I want that if I receive "NDS" from Web Service then its show "NDS" RadioButton Checked or if I receive "DS" then its show "DS" checked. how can I achieve this?
Below is my Radio Group XML Code:
<RadioGroup android:id="#+id/rdoCnsumerCatgory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<RadioButton android:id="#+id/rdoDS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="DS"
android:layout_marginLeft="20dp"
android:textColor="#000000"
android:textSize="10sp"
android:textStyle="bold" />
<RadioButton android:id="#+id/rdoNDS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:gravity="center"
android:text="NDS"
android:textColor="#000000"
android:textSize="10sp"
android:textStyle="bold" />
<RadioButton android:id="#+id/rdoSip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:gravity="center"
android:text="SIP"
android:textColor="#000000"
android:textSize="10sp"
android:textStyle="bold" />
<RadioButton android:id="#+id/rdoMip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginLeft="20dp"
android:text="MIP"
android:textColor="#000000"
android:textSize="10sp"
android:textStyle="bold" />
<RadioButton android:id="#+id/rdoMl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="ML"
android:layout_marginLeft="20dp"
android:textColor="#000000"
android:textSize="10sp"
android:textStyle="bold" />
</RadioGroup>
Try This
By if-else method
String mValue="";
RadioButton rdoMl,rdoMip,rdoSip,rdoNDS,rdoDS;
if(mValue.equalsIgnoreCase("NDS"))
{
rdoNDS.setChecked(true);
}
else if(mValue.equalsIgnoreCase("DS"))
{
rdoDS.setChecked(true);
}
else if(mValue.equalsIgnoreCase("MIP"))
{
rdoMip.setChecked(true);
}
else if(mValue.equalsIgnoreCase("SIP"))
{
rdoSip.setChecked(true);
}
else if(mValue.equalsIgnoreCase("ML"))
{
rdoMl.setChecked(true);
}
By switch method
switch(mValue) {
case "NDS":
rdoNDS.setChecked(true);
break;
case "DS":
rdoDS.setChecked(true);
break;
case "MIP":
rdoMip.setChecked(true);
break;
case "SIP":
rdoSip.setChecked(true);
break;
case "ML":
rdoMl.setChecked(true);
break;
// etc...
}

Two RadioButtons are selected

Why am I getting two RadioButtons selected?
I want only one RadioButton selected at a time.
Here is the layout:
<RadioGroup
android:orientation="vertical"
android:id="#+id/radio_gender"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checkedButton="#+id/radio_female"
android:layout_alignRight="#+id/Button01"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/button1" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio_female"
android:text="#string/radio_female" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio_male"
android:text="#string/radio_male" />
and the code:
public void radioButtonClicked(View view) {
// Check that the button is now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.radio_female:
if (checked)
userGender = "female";
break;
case R.id.radio_male:
if (checked)
userGender = "male";
break;
}
}
You need to put the RadioButton inside the RadioGroup. Change your XML like this:
<RadioGroup
android:orientation="vertical"
android:id="#+id/radio_gender"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checkedButton="#+id/radio_female"
android:layout_alignRight="#+id/Button01"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/button1">
<RadioButton
android:onClick="radioButtonClicked"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio_female"
android:text="#string/radio_female" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio_male"
android:text="#string/radio_male" />
</RadioGroup>
Close RadioGroup tag
<RadioGroup
android:orientation="vertical"
android:id="#+id/radio_gender"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checkedButton="#+id/radio_female"
android:layout_alignRight="#+id/Button01"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/button1" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio_female"
android:text="#string/radio_female" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio_male"
android:text="#string/radio_male" />
</RadioGroup>

How to create buttons like keyboard keys in android?

This question seems trivial. I want to create a button like keyboard keys for my app. When I click on it, a popup window appears above that button showing the letter pressed. Everything works great till now except one thing. When I add onFocusChangedListener to the button, nothing happens. I need to let my button act as a keyboard key, but I don't know how.
As you can see here, when a button is focused, a popup window appears. I want to do that, but onFocusChangeListener doesn't work. I know I can use a KeyboardView to achieve that, but I don't want to use that due to some other issues like centering buttons and setting keys' height with layout_weight. So I need to make it with normal buttons.
What I tried:
My First Try:
button.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
popupWindow.showAtLocation(keyboardPopup, Gravity.NO_GRAVITY, location.left - 10, location.top - button.getHeight());
} else {
popupWindow.dismiss();
}
}
});
Result: Nothing happened. The popup window didn't appear at all.
Edit: After I have added button.setFocusableInTouchMode(true); as Ashley suggested, onFocusChanged is now getting called, but it acts so weird. The popup is sometimes shown, but at the same time when it is shown, it never disappears...
My Second Try:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
popupWindow.showAtLocation(keyboardPopup, Gravity.NO_GRAVITY, location.left - 10, location.top - button.getHeight());
break;
case MotionEvent.ACTION_UP:
popupWindow.dismiss();
break;
}
return true;
}
});
Result: This one acted so weird. Sometimes the popup shows and sometimes not, but when it is shown, the button didn't also change its state. It should have been focused, but nothing happened to the button, it acts as if it was in a normal state (Button's background doesn't change with state_focused declared in my drawable xml). It seems that onTouchListener overrides the button's functionality.
Here is a part of my layout:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Q"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="W"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="E"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="R"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="T"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Y"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="U"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="I"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="O"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="P"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.5" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="A"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="S"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="D"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="F"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="G"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="H"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="J"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="K"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="L"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.5" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1.5" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Z"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="X"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="C"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="V"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="B"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="N"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="M"
android:background="#drawable/keyboard_button"
android:textColor="#FFFFFF"
android:onClick="onKeyboardClick" />
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1.5" />
</LinearLayout>
</LinearLayout>
In code:
public void onKeyboardClick(View view) {
//The view pressed is a button.
final Button button = (Button) view;
//Create a PopupWindow.
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View keyboardPopup = inflater.inflate(R.layout.keyboard_popup, null);
final PopupWindow popupWindow = new PopupWindow(keyboardPopup, view.getWidth() + 20, view.getHeight());
TextView keyboardKey = (TextView) keyboardPopup.findViewById(R.id.keyboard_key);
keyboardKey.setText(button.getText().toString());
//Get button location to show the popup above it.
int[] keyLocation = new int[2];
button.getLocationOnScreen(keyLocation);
final Rect location = new Rect();
location.left = keyLocation[0];
location.top = keyLocation[1];
location.right = location.left + button.getWidth();
location.bottom = location.top + button.getHeight();
//This is a temporary solution. I don't want to use that.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Show popup.
popupWindow.showAtLocation(keyboardPopup, Gravity.NO_GRAVITY, location.left - 10, location.top - button.getHeight());
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Dismiss popup.
popupWindow.dismiss();
}
}, 200);
}
});
}
Any help will be greatly appreciated. Thanks.
I suggest you use the second try onTouchListener!
You had 2 issues with that:
1. The button does not change state
Indeed, when you override the onTouchListener you must simulate the state yourself. Please take a look at this SO thread for how it is done:
"Press and hold" button on Android needs to change states (custom XML selector) using onTouchListener
2. Sometime shows and sometimes not
This should not happen, especially if you handle all the relevant touch event cases properly. You will want to show the pop up when the user touch down on a button and hide that pop up when a user move out of the button (either by swiping out or taking the finger off the screen).
Please try the following code sample:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
v.setPressed(true);
showPopupWindow(...);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
v.setPressed(false);
hidePopupWindow(...);
break;
}
return true;
}
});
Notice the use of getActionMasked instead of getAction to better handle multi touch.
Notice the v.setPressed(...); - this will update the button state.
Notice the different cases of hiding the pop up.
Instead of setting an OnTouchListener, try subclassing Button and overriding onTouchEvent itself:
public class KeyboardButton extends Button
{
#Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
popupWindow.showAtLocation(keyboardPopup, Gravity.NO_GRAVITY, location.left - 10, location.top - button.getHeight());
break;
case MotionEvent.ACTION_UP:
popupWindow.dismiss();
break;
}
return super.onTouchEvent(event);
}
}
I didn't notice it in your code but potentially you left out.
btn.setFocusableInTouchMode(true);
btn.setFocusable(true);
For the on focus change listener.
holder.sAttedenceList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (!parent.hasFocus()) {
return;
}
not the full code but this the general structure that worked for me.

Adding RadioButtons in Android

I want to add radio buttons such that only one of them is selected at a time among 4 buttons and I want to place them as:
RadioButton1 RadioButton2
RadioButton3 RadioButton4
I am trying the following code but 1&2 forms a grp and 3&4 forms a different grp and there are two value selected a time. Can anyone check it and share the correct way to do it?
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/apple" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/rdogrp_main"
android:orientation="vertical"
>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rdogrp1"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange"
android:textColor="#000000"
/>
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="BlueBerry"
android:textColor="#000000"
/>
</RadioGroup>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rdogrp2"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple "
android:textColor="#000000"
/>
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Santra :P"
android:textColor="#000000"
/>
</RadioGroup>
</RadioGroup>
</LinearLayout>
Ok I find a solution for you:
I know it is not the best solution but it works! :)
Just copy the xml file and activity code bellow:
Activity:
package com.example.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RadioButton;
public class MainActivity extends Activity {
private static String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RadioButton radiobutton1 = (RadioButton) findViewById(R.id.RadioButton1);
final RadioButton radiobutton2 = (RadioButton) findViewById(R.id.RadioButton2);
final RadioButton radiobutton3 = (RadioButton) findViewById(R.id.RadioButton3);
final RadioButton radiobutton4 = (RadioButton) findViewById(R.id.RadioButton4);
radiobutton1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton1.setChecked(true);
radiobutton2.setChecked(false);
radiobutton3.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton2.setChecked(true);
radiobutton1.setChecked(false);
radiobutton3.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton3.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton3.setChecked(true);
radiobutton1.setChecked(false);
radiobutton2.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton4.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton4.setChecked(true);
radiobutton1.setChecked(false);
radiobutton2.setChecked(false);
radiobutton3.setChecked(false);
return true;
}
});
}
}
activity_main.xml :
<RelativeLayout 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"
tools:context=".MainActivity" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#FFDDDDDD" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioGroup
android:id="#+id/rdogrp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange"
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="BlueBerry"
android:textColor="#000000" />
</RadioGroup>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioGroup
android:id="#+id/rdogrp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple "
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Santra :P"
android:textColor="#000000" />
</RadioGroup>
</TableRow>
</TableLayout>
</RelativeLayout>
For that you need to create only one RadioGroup and add four different RadioButton.
And for adding UI refer this url : Manage Layout Inside a Horizontal Oriented Radiogroup .
I hope this will help you.
Try Like this..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/rdogrp_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="India"
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="74dp"
android:layout_height="wrap_content"
android:layout_weight="0.57"
android:text="Austraila"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Srilanka "
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.48"
android:text="SouthAfrica"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
Let me know if you solve the problem...

Button to change imageview on main.xml (android)

(My first post, btw)
My problem is so newbie-esque that I can't find anyone who has asked it and oh have I tried. So here goes..:
My main screen has an imageview. Click it and it sends you to a different layout from where a bunch of images (imagebuttons) can be selected (works fine).
Pressing any of these imagebuttons should send that image to my main imageview (crashing)
I made an imageview inside this layout where the ImButtons are and sent the images there instead. This is working fine so the switch statements are ok.
I have tried to make my main imageview a "public" in my main.java but still crashing.
So the question:
How do I send an image from one layout/class to another?
Grateful in advance.
Jakob
Some code:
My secondary layout:
package egen.helt.min;
import android.app.Activity;``
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
public class FartSelect extends Activity implements OnClickListener {
public ImageView VælgMax;
MediaPlayer mpButtonClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fartselect);
VælgMax = (ImageView) findViewById(R.id.ivValgtMax);
ImageButton skilt30 = (ImageButton) findViewById(R.id.ib30s);
ImageButton skilt40 = (ImageButton) findViewById(R.id.ib40s);
ImageButton skilt50 = (ImageButton) findViewById(R.id.ib50s);
ImageButton skilt60 = (ImageButton) findViewById(R.id.ib60s);
ImageButton skilt70 = (ImageButton) findViewById(R.id.ib70s);
ImageButton skilt80 = (ImageButton) findViewById(R.id.ib80s);
ImageButton skilt90 = (ImageButton) findViewById(R.id.ib90s);
ImageButton skilt100 = (ImageButton) findViewById(R.id.ib100s);
ImageButton skilt110 = (ImageButton) findViewById(R.id.ib110s);
ImageButton skilt120 = (ImageButton) findViewById(R.id.ib120s);
ImageButton skilt130 = (ImageButton) findViewById(R.id.ib130s);
skilt30.setOnClickListener(this);
skilt40.setOnClickListener(this);
skilt50.setOnClickListener(this);``
skilt60.setOnClickListener(this);
skilt70.setOnClickListener(this);
skilt80.setOnClickListener(this);
skilt90.setOnClickListener(this);
skilt100.setOnClickListener(this);
skilt110.setOnClickListener(this);
skilt120.setOnClickListener(this);
skilt130.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.ib30s:
VælgMax.setImageResource(R.drawable.skilt30s);
break;
case R.id.ib40s:
VælgMax.setImageResource(R.drawable.skilt40s);
break;
case R.id.ib50s:
VælgMax.setImageResource(R.drawable.skilt50s);
break;
case R.id.ib60s:
VælgMax.setImageResource(R.drawable.skilt60s);
break;
case R.id.ib70s:
VælgMax.setImageResource(R.drawable.skilt70s);
break;
case R.id.ib80s:
VælgMax.setImageResource(R.drawable.skilt80s);
break;
case R.id.ib90s:
VælgMax.setImageResource(R.drawable.skilt90s);
break;
case R.id.ib100s:
VælgMax.setImageResource(R.drawable.skilt100s);
break;
case R.id.ib110s:
VælgMax.setImageResource(R.drawable.skilt110s);
break;
case R.id.ib120s:
VælgMax.setImageResource(R.drawable.skilt120s);
break;
case R.id.ib130s:
VælgMax.setImageResource(R.drawable.skilt130s);
break;
}
}
}
And here is FartSelect.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivValgteMax"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/blanktskiltsk" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="400dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib30s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt30s" />
<ImageButton
android:id="#+id/ib40s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt40s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib50s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt50s" />
<ImageButton
android:id="#+id/ib60s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt60s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib70s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt70s" />
<ImageButton
android:id="#+id/ib80s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt80s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib90s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt90s" />
<ImageButton
android:id="#+id/ib100s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt100s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib110s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt110s" />
<ImageButton
android:id="#+id/ib120s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt120s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib130s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt130s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib30sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt30sk" />
<ImageButton
android:id="#+id/ib40sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt40sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib50sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt50sk" />
<ImageButton
android:id="#+id/ib60sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt60sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib70sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt70sk" />
<ImageButton
android:id="#+id/ib80sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt80sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib90sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt90sk" />
<ImageButton
android:id="#+id/ib100sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt100sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib110sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt110sk" />
<ImageButton
android:id="#+id/ib120sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt120sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib130sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt130sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt30" />
<ImageButton
android:id="#+id/ib40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt40" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt50" />
<ImageButton
android:id="#+id/ib60"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt60" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib70"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt70" />
<ImageButton
android:id="#+id/ib80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt80" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib90"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt90" />
<ImageButton
android:id="#+id/ib100"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt100" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib110"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt110" />
<ImageButton
android:id="#+id/ib120"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt120" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib130"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt130" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Within the same layout the imageview can change. Just not when sent to an imageview on main layout.
You're not supposed to sent images between activities, if that's what you mean. You could probably do it, but it would definitely be slow through something like an intent. From what I understand, you want to go to Activity B and then click an ImageButton, then return back to Activity A and have that displayed in the ImageView. Couldn't you just pass in the path of the image through an intent (i.e. as a String)? Alternatively, you could just pass in the name as a String and then display that image in the next Activity by setting the source of the ImageView in Activity A.
If the image was created temporarily or something, and you needed to pass it, you could just save it to a private file (MODE_PRIVATE) and then access it in the next activity.
Sorry if I'm thinking something off topic.
EDIT
Look at this for more information.
EDIT 2
You can use intents to pass Strings between activities: (i.e. the image path/name)
String myImageName = "image1";
Intent picIntent = new Intent(this, NextActivity.class);
picIntent.putExtra("name", myImageName); //this has to correspond to the below name
startActivity(intent);
You can retrieve it in the next activity:
String thePictureName = getIntent().getStringExtra("name"); //correspond to above name
You don't send an ImageView from one layout to another.
You may either,
Remove the ImageView on layout and add to the other layout (by inflating).
Hide on first layout and show on other layout.
Set up an intent i for the FartSelector
startActivity(i);
Create an OnActivityResult(int result_code) and check for the FartSelector's code
In fart selector, pack a new intent with the desired image data on click
Call setResult(newIntent); and finish(); after creating and packing the intent
Picking up where we left off at 3, unpack the intent and use it in your ImageView.
Your problem is this
VælgMax = (ImageView) findViewById(R.id.ivValgtMax);
ivValgtMax is not an id that is defined in fartlayout therefore that code will give you null. I assume ivValgtMax refers to an ImageView from another layout.
If I understand you correctly, you want to launch the Activity B (which has the images) from another Activity A (which has the main ImageView), select an image while in Activity B, and return the selected image to Activity A after the user has clicked.
If all of this is correct, we can proceed. The way you want to launch Activity B from A is to use something like
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, IMAGE_SELECTION_VALUE);
where IMAGE_SELECTION_VALUE is a static value that you assign an integer to, just make it something unique.
In Activity B, once the user finishes clicking, your goal is to return the selection to Activity A. To do this, you need to create an intent and set the result.
public void onClick(View v) {
... your code ...
Intent resultIntent = new Intent(null);
resultIntent.putExtra(IMAGE_FIELD_NAME, userSelection);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
In the above code, IMAGE_FIELD_NAME will be a static String that you can use to find the correct image (we will find how to use it in the next step). userSelection will be the data that you send back to Activity A. You can use v.getId() for this and move your switch statement to Activity A.
Lastly, you will want to override onActivityResult in Activity A. This will allow you to get the result from Activity B and is where we will use IMAGE_FIELD_NAME.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case IMAGE_SELECTION_VALUE:
if (resultCode == Activity.RESULT_OK) {
int imageId = data.getIntExtra(IMAGE_FIELD_NAME);
// now you can update your ImageView here
}
break;
default:
break;
}
}

Categories

Resources