How to create buttons like keyboard keys in android? - 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.

Related

Transparent ImageView does not react for click

I'd like to use a Google Map inside a SrcollView. To that I need to use something like that:
<ScrollView
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:fillViewport="true"
android:id="#+id/scrollView">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:id="#+id/relativeinfo1">
<View
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_above="#+id/titleinfo"
android:layout_toRightOf="#+id/imageinfo"/>
<TextView
android:id="#+id/titleinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_toRightOf="#+id/imageinfo"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="phone"
android:id="#+id/mooobil"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="phone"
android:id="#+id/telefoneeo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="email"
android:id="#+id/emailese"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:id="#+id/honlapese"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/imageinfo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"/>
<View
android:layout_width="wrap_content"
android:layout_height="15dp"
android:id="#+id/view1"
android:layout_below="#+id/imageinfo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cimtext"
android:textStyle="bold"
android:text="Cím: "
android:textSize="15dp"
android:textColor="#000000"
android:visibility="invisible"/>
<View
android:layout_width="wrap_content"
android:layout_height="10dp"
android:id="#+id/vonal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#FFD700" />
<View
android:layout_width="wrap_content"
android:layout_height="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/vonal2"
android:background="#FFD700"/>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="300dp"
tools:context=".MainActivity"
android:id="#+id/relativemaplayout">
<fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
class="com.google.android.gms.maps.MapFragment" />
<ImageView
android:id="#+id/transparent_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick"
android:background="?android:selectableItemBackground"/>
</RelativeLayout>
</RelativeLayout>
View v = inflater.inflate(R.layout.activity_service_info,parent, false);
mScrollView = (ScrollView) v.findViewById(R.id.scrollView);
transparentImageView = (ImageView) v.findViewById(R.id.transparent_image);
mScrollView.requestDisallowInterceptTouchEvent(true);
transparentImageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
Log.d("action","down");
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
mScrollView.requestDisallowInterceptTouchEvent(true);
Log.d("action","down");
return false;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
mScrollView.requestDisallowInterceptTouchEvent(false);
Log.d("action","up");
return true;
case MotionEvent.ACTION_MOVE:
mScrollView.requestDisallowInterceptTouchEvent(true);
Log.d("action","move");
return false;
default:
return true;
}
}
});
transparentImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "anyád", Toast.LENGTH_SHORT).show();
}
});
I don't know why but the transparent ImageView does not respond for click. What should I do to make the ImageView react for the click? I have tried some other way to make the ScrollView work but non of them worked and this solution looks pretty good.

best way to implement pop up window in android application

I have the main activity in which I have few menu buttons. When the user presses specific button I want to open a new activity which also have a couple of buttons which I need to handle their click. In other words, I need to pop up window functionality as normal activity.
I looked online and found several ways to implement this such as: just customising the size of the activity, use diaglog theme in the manifest, use it as a fragment or use Popup Window Class. But as I am new to android I want the best way to implement it for my project.
Can someone help me achieve this?
EDIT:
this is the xml file i want to use in the pop up window (for better explanation of what i want to achieve):
<?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:gravity="center_horizontal"
android:orientation="vertical"
android:background="#0091cb"
android:padding="16dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="#dimen/activity_horizontal_margin"
android:weightSum="3"
android:id="#+id/check">
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/computer"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button1"
android:textSize="10dp"
android:textColor="#fff"
android:layout_weight="1"
android:onClick="button1_OnClick"/>
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/electrical"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button2"
android:textSize="10dp"
android:textColor="#fff"
android:layout_weight="1"
android:onClick="button2_OnClick"/>
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/hdtv"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button3"
android:textSize="10dp"
android:textColor="#fff"
android:layout_weight="1"
android:onClick="button3_OnClick"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:orientation="horizontal"
android:layout_below="#id/check"
android:paddingTop="10dp">
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/bill"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button4"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button4_OnClick"/>
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="10dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/water"
android:paddingTop="12dp"
android:text="button5"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button5_OnClick" />
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="10dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/electrical"
android:paddingTop="12dp"
android:text="button6"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button6_OnClick" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:orientation="horizontal">
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="10dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/notepad"
android:paddingTop="7dp"
android:text="button7"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button7_OnClick" />
</LinearLayout>
</LinearLayout>
Depends on your needs.
If you need just to show 'classic' popup window (error while typing in input field or small color picker) - use PopupWindow class. You can use my gist.
If your message is more general (e.g. 'There is no internet connection') or user should choose yes/no - use dialog. There is cool library.
Activity with custom size is used rarely. Maybe in some dial applications.
So choose implementation depends on your needs.
create method where u want to open popup windiw in your activity
Like this,here p is a specific point(location) where you want exactly open your window.
final Point p = new Point();
show_popup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showpopupwindows(Activity, p);
}
});
then,
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int location[] = new int[2];
show_popup.getLocationOnScreen(location);
p.x = location[0];
p.y = location[1];
}
private void showpopupwindows(final Activity context, Point p) {
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popup_menu);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Display display = context.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int popupwidth = (size.x / 2);
int popupheight =(size.y / 2);
View layout = layoutInflater.inflate(R.layout.detail_pop, viewGroup);
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupwidth);
popup.setHeight(popupheight);
popup.setFocusable(true);
popup.setAnimationStyle(R.style.WindowAnimation);
popup.setBackgroundDrawable(new ColorDrawable(
android.graphics.Color.TRANSPARENT));
popup.showAtLocation(layout, Gravity.NO_GRAVITY, popupwidth,popupheight);
detail_pop1 = (TextView) layout.findViewById(R.id.detail_pop1);
detail_pop1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "pop window is opened, Toast.LENGTH_SHORT).show();
}
});
}
detail_pop.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_menu"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#color/skyblue"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:id="#+id/detail_pop1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:drawableTop="#drawable/ic_launcher"
android:gravity="center"
android:text="P"
android:textSize="#dimen/font_24" />
</LinearLayout>

how to hide nested layout in android?

I have an linear layout in my application,In that i am using one root layout(invitation_single) and one nested layout(hidden).when i am onclick root layout at run time the nested layout visible(it contains"yes,no,maybe" buttons)successfully.Now my need is if i have similar layout values(consider event 2) immediately below the previous layout values(consider event 1),when i am on click "event 2" i need to hide nested layout of "event 1"(atpresent when i am click button the nested layout gone)automatically.how can i achieve this..here is my layout code,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/invitation_single"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:dividerVertical"
android:dividerPadding="5dp"
android:showDividers="middle"
tools:context=".MainActivity">
<ImageButton
android:id="#+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_action_event" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="true"
android:orientation="vertical">
<TextView
android:id="#+id/invitation_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingTop="3dp"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/invitation_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:textColor="#color/black"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/hidden"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="-270dp"
android:layout_marginTop="60dp"
android:layout_weight="1"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:paddingTop="1dp"
android:visibility="gone"
android:weightSum="3">
<Button
android:id="#+id/yesbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="7dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Yes"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/nobutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="No"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/buttonmaybe"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Maybe"
android:textColor="#color/black"></Button>
</LinearLayout>
</LinearLayout>
my programming code is below,
final LinearLayout first = (LinearLayout) convertView.findViewById(R.id.invitation_single);
final LinearLayout second = (LinearLayout) convertView.findViewById(R.id.hidden);
first.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.invitation_single:
second.setVisibility(View.VISIBLE);
break;
}
}
});
is this what you're looking for?
first.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.invitation_single:
second.setVisibility(second.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
break;
}
}
});

Multiple row Radio buttons in 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");

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