I have 2 layouts in my activity, if clicked on ImageView "show" from 1st layout, 2nd layout should display, but here its not happening so, had given the coding correcting but could not solve this issue
Here's my code
public class About extends Activity {
LinearLayout line1, line2;
ImageView show;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
line1=(LinearLayout) findViewById(R.id.ll1);
line2=(LinearLayout)findViewById(R.id.ll2);
show=(ImageView)findViewById(R.id.ss);
show.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
show.setVisibility(View.VISIBLE);
// TODO Auto-generated method stub
line1.setVisibility(View.VISIBLE);
if (line2.getVisibility() == View.INVISIBLE || line2.getVisibility() == View.GONE) {
line2.setVisibility(View.VISIBLE); }
else {
line2.setVisibility(View.INVISIBLE);
}
}
});
Xml Layout
<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"
android:background="#color/black" >
<LinearLayout
android:id="#+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/black"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
<ImageView
android:id="#+id/ss"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/menu"
android:clickable="true" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:background="#color/black"
android:layout_toRightOf="#+id/ll1"
android:visibility="gone"
>
<ImageView
android:id="#+id/about"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="#+id/textView1"
android:layout_toLeftOf="#+id/jobs"
android:src="#drawable/about" />
<ImageView
android:id="#+id/jobs"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/jobs" />
<ImageView
android:id="#+id/log"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/log" />
<ImageView
android:id="#+id/home"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/home" />
</LinearLayout>
</RelativeLayout>
For your image view 'ss' in xml, set property android:clickable="true".
Or better otherwise, use an ImageButton in place of ImageView in your xml
EDIT -
Change this
show=(ImageView)findViewById(R.id.ss);
to
show=(ImageView)findViewById(R.id.show);
You have wrongly defined the id of your ImageView that is why you are not able to get the onClick event of your ImageView.
Change the id of your ImageView to R.id.show besides R.id.ss as below:
Change this
show=(ImageView)findViewById(R.id.ss);
to
show=(ImageView)findViewById(R.id.show);
EDITED:
Try to show and hide your layout as below in your onClick listener.
if (line2.getVisibility() == View.INVISIBLE || line2.getVisibility() == View.GONE) {
line2.setVisibility(View.VISIBLE);
line1.setVisibility(View.INVISIBLE);
}
else {
line2.setVisibility(View.INVISIBLE);
line1.setVisibility(View.VISIBLE);
}
Related
Basically, I want to have an AlertDialog in which multiple images will load on the same ImageView. By clicking the NEXT button it will get the next image bitmap from bitmap list and finally will load on that ImageView. Same case for PREV button. It will take previous bitmap and will load the image on the same ImageView.
But the problem is after loading the first image it does not load the next image. If I click the next button then it takes the next bitmap but the imageview.setImageBitmap(bitmap) does not work.
How to remove or clear the previous image to place the next photos?
The XML file are given below :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/question"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:text="#string/question"
android:textSize="14sp"
android:textColor="#color/light_black"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#color/divider"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="0dp"
app:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/selected_place_images"
android:scaleType="centerCrop"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#color/divider"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="47dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/previous"
android:text="#string/previous_page"
android:textColor="#color/black"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/next"
android:text="#string/next_page"
android:textColor="#color/black"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
The java code are given below :
private void showDialogOfImages() {
Log.d(TAG,"showDialogOfImages : showing places images");
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = this.getLayoutInflater();
View customLayout = inflater.inflate(R.layout.custom_displaying_selected_place_images,null);
mSelectedPlaceImages = (ImageView) customLayout.findViewById(R.id.selected_place_images);
nextPage = (TextView) customLayout.findViewById(R.id.next);
previousPage = (TextView) customLayout.findViewById(R.id.previous);
displayPhoto();
nextPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentPhotoIndex++;
displayPhoto();
}
});
previousPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentPhotoIndex--;
displayPhoto();
}
});
builder.setView(customLayout);
builder.show();
}
private void displayPhoto() {
if (mCurrentPhotoIndex < mSelectedLocationPhotosBitmap.size()) {
Bitmap bitmap = mSelectedLocationPhotosBitmap.get(mCurrentPhotoIndex);
Toast.makeText(context,""+mCurrentPhotoIndex+" : "+bitmap,Toast.LENGTH_LONG).show();
mSelectedPlaceImages.setImageBitmap(bitmap);
setButtonVisibility();
}
}
private void setButtonVisibility() {
if(mCurrentPhotoIndex == 0 && mSelectedLocationPhotosBitmap.size() == 1){
nextPage.setEnabled(false);
nextPage.setClickable(false);
nextPage.setTextColor(getResources().getColor(R.color.divider));
previousPage.setEnabled(false);
previousPage.setClickable(false);
previousPage.setTextColor(getResources().getColor(R.color.divider));
}
else if (mCurrentPhotoIndex == 0 && mSelectedLocationPhotosBitmap.size() > 1){
nextPage.setEnabled(true);
nextPage.setClickable(true);
nextPage.setTextColor(getResources().getColor(R.color.black));
previousPage.setEnabled(false);
previousPage.setClickable(false);
previousPage.setTextColor(getResources().getColor(R.color.divider));
}
else if (mCurrentPhotoIndex == mSelectedLocationPhotosBitmap.size()-1 && mSelectedLocationPhotosBitmap.size() > 1){
nextPage.setEnabled(false);
nextPage.setClickable(false);
nextPage.setTextColor(getResources().getColor(R.color.divider));
previousPage.setEnabled(true);
previousPage.setClickable(true);
previousPage.setTextColor(getResources().getColor(R.color.black));
}
else{
nextPage.setEnabled(true);
nextPage.setClickable(true);
nextPage.setTextColor(getResources().getColor(R.color.black));
previousPage.setEnabled(true);
previousPage.setClickable(true);
previousPage.setTextColor(getResources().getColor(R.color.black));
}
}
You need to clear the previous image from imageview and then you can set the new on top.
Do as below
private void displayPhoto() {
if (mCurrentPhotoIndex < mSelectedLocationPhotosBitmap.size()) {
Bitmap bitmap = mSelectedLocationPhotosBitmap.get(mCurrentPhotoIndex);
Toast.makeText(context,""+mCurrentPhotoIndex+" : "+bitmap,Toast.LENGTH_LONG).show();
mSelectedPlaceImages.setImageBitmap(null)
mSelectedPlaceImages.setImageBitmap(bitmap);
setButtonVisibility();
}
}
Try this:
mSelectedPlaceImages.destroyDrawingCache();
mSelectedPlaceImages.setImageBitmap(bitmap);
mSelectedPlaceImages.buildDrawingCache();
I have Following layout Scenario
Want to implement the Click even of TextView ..but the TextView Click not working
Here is code Sample
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:clickable="false">
<customview
android:id="#+id/custom"
android:layout_width="200dp"
android:layout_height="200dp"
android:clickable="false"/>
<LinearLayout
android:id="#+id/abc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/zero_done"
android:padding="5dp"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
My TextView ClickEvent not working ...please help me
Class Code
TextView view= (TextView) findViewById(R.id.btn);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Press",
Toast.LENGTH_SHORT).show();
}
});
You didn't call show method of Toast,so you have no idea whether TextView click event did happen or not.
Try this:
Toast.makeText(getApplicationContext(), "Press",
Toast.LENGTH_SHORT).show(); // don't miss show method.
I am using Vuforia AR sdk and want to create a button on the camera preview on the screen.
I cannot figure out where and how to add the button.
I have edit the camera_overlay_udt.xml like this.. In my layout design i have placed back button and listview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/header"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#android:color/transparent"
android:src="#drawable/back" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Swipart"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/arcstarButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:background="#android:color/transparent"
android:src="#drawable/star_button" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/favListingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/headerLayout"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ListView
android:id="#+id/favlist"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="7dp"
android:cacheColorHint="#00000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="#color/overlay_bottom_bar_background"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1" >
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/overlay_bottom_bar_separators" />
<ImageButton
android:id="#+id/camera_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:contentDescription="#string/content_desc_camera_button"
android:onClick="onCameraClick"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:src="#drawable/camera_button_background" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#id/bottom_bar"
android:background="#color/overlay_bottom_bar_separators" />
</RelativeLayout>
after that please Edit that ImageTargets.java class
private void addOverlayView(boolean initLayout) {
// Inflates the Overlay Layout to be displayed above the Camera View
LayoutInflater inflater = LayoutInflater.from(this);
mUILayouts = (RelativeLayout) inflater.inflate(
R.layout.camera_overlay_udt, null, false);
mUILayouts.setVisibility(View.VISIBLE);
// If this is the first time that the application runs then the
// uiLayout background is set to BLACK color, will be set to
// transparent once the SDK is initialized and camera ready to draw
if (initLayout) {
mUILayouts.setBackgroundColor(Color.TRANSPARENT);
}
// Adds the inflated layout to the view
addContentView(mUILayouts, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Gets a reference to the bottom navigation bar
mBottomBar = mUILayouts.findViewById(R.id.bottom_bar);
// Gets a reference to the Camera button
mCameraButton = mUILayouts.findViewById(R.id.camera_button);
mCameraButton.setVisibility(View.GONE);
favButton = (ImageButton) mUILayouts.findViewById(R.id.arcstarButton);
listview = (ListView) mUILayouts.findViewById(R.id.favlist);
backButton = (ImageButton) mUILayouts.findViewById(R.id.backButton);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
finish();
}
});
listview.setVisibility(View.GONE);
galleryList = SendFile.getFavourites();
if (galleryList != null) {
gridviewAdapter = new GridviewAdapter(ImageTargets.this);
listview.setAdapter(gridviewAdapter);
}
favButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (galleryList != null && galleryList.size() > 0) {
if (listview.getVisibility() == View.GONE) {
listview.setVisibility(View.VISIBLE);
} else {
listview.setVisibility(View.GONE);
}
} else {
Toast.makeText(ImageTargets.this, "Favourites not fond",
Toast.LENGTH_LONG).show();
}
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> paramAdapterView,
View paramView, int positon, long paramLong) {
SendFile.setFavourite(galleryList.get(positon));
Intent intent = new Intent(ImageTargets.this,
LoadingScreen.class);
Bundle bundle = new Bundle();
bundle.putInt("x", x_Axis);
bundle.putInt("y", y_Axis);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
});
showDialogHandler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Server Response: " + aResponse, Toast.LENGTH_SHORT)
.show();
showAlertDialog(aResponse);
} else {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Not Got Response From Server.", Toast.LENGTH_SHORT)
.show();
}
};
};
loadingDialogHandler.captureButtonContainer = mUILayouts
.findViewById(R.id.camera_button);
mUILayouts.bringToFront();
}
They showing there layouts using handlers
Start you camera preview in a normal way. Place a layout on top of it with transparent background like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff000000"
android:layout_height="match_parent">
<ImageView
android:id="#+id/start_image_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:layout_weight="1"
android:src="#drawable/scan_image"/>
</RelativeLayout>
In java file, you can add this layout like this:
private View mStartupView;
mStartupView = getLayoutInflater().inflate(
R.layout.startup_screen, null);
// Add it to the content view:
addContentView(mStartupView, new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
This way you will get to see your button on top of camera preview. Hope it helps
You can add buttons in cameraoverlay layout which is in layout folder and you can initialize buttons in initAR function which is in mainactivity.
Step 1: Add the button in the camera_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
style="#android:style/Widget.ProgressBar"
android:id="#+id/loading_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="51dp"
android:text="Button" />
</RelativeLayout>
Step 2: Edit the ImageTargets.java class
private static final String LOGTAG = "ImageTargets";
private Button b1;
Step 3: Modify the initApplicationAR() function of ImageTargets.java class
private void initApplicationAR()
{
// Create OpenGL ES view:
int depthSize = 16;
int stencilSize = 0;
boolean translucent = Vuforia.requiresAlpha();
mGlView = new SampleApplicationGLView(this);
mGlView.init(translucent, depthSize, stencilSize);
mRenderer = new ImageTargetRenderer(this, vuforiaAppSession);
mRenderer.setTextures(mTextures);
mGlView.setRenderer(mRenderer);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setVisibility(View.GONE);
}
});
}
Now lay back and watch your button disappear on a click!
Although it's a long time since the post.. yet I found one article.. wherein you can have the desired thing..
Ref: https://medium.com/nosort/adding-views-on-top-of-unityplayer-in-unityplayeractivity-e76240799c82
Solution:
Step1: Make a custom layout XML file (vuforia_widget_screen.xml). For example, button has been added.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout">
<FrameLayout
android:id="#+id/unity_player_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#null"
android:text="#string/welcome" />
</FrameLayout>
Step 2: Make following changes in the UnityPlayerActivity.java
Replace "setContentView(mUnityPlayer);" with
setContentView(R.layout.vuforia_widget_screen);
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.addView(mUnityPlayer.getView());
-> For anyone, who will face the issue in future. :)
I've a one ListView as shown below, when the program is first executed,then it is visible
later automatically invisible.
This layout is visible when User clicks the Button, before that the Visibility is GONE
<RelativeLayout
android:id="#+id/comments_block"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:visibility="gone" >
<TextView
android:id="#+id/comments_subHeadding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Type your comment"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/red" />
<EditText
android:id="#+id/user_commetns"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_below="#+id/comments_subHeadding"
android:layout_marginTop="5dip"
android:background="#color/white"
android:imeOptions="actionSend"
android:inputType="textLongMessage" />
</RelativeLayout>
This Layout contains ListView Here I'm facing the problem.
<LinearLayout
android:id="#+id/listview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/comments_block"
android:background="#color/ios_blue"
android:orientation="vertical">
<ListView
android:id="#+id/comments_list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dip"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent" >
</ListView>
</LinearLayout>
Activity Code
#Override
public void onClick(View v)
{
if (v == add_comment_button)
{
if(show)
{
comments_block.setVisibility(View.VISIBLE);
show = false;
return;
}
else
{
comments_block.setVisibility(View.GONE);
show = true;
}
}
else if (v == back_comments_button)
{ finish(); }
}
In your code you are doing
android:layout_height="wrap_content", if there are no item in listview then its height will be 0.
So please if you will use match_parent instead of wrap_content
android:layout_height="match_content"
android:layout_weight="1"
Then your listview will be visible.
I hope it will work.
I found the solution for your problem.....
You are just showing and hiding the comments_block only....
you try this one...I hope it will help you definitely...All the bset
#Override
public void onClick(View v)
{
if (v == add_comment_button)
{
if(show)
{
comments_block.setVisibility(View.VISIBLE);
listview_layout.setVisibility(View.GONE);
show = false;
return;
}
else
{
comments_block.setVisibility(View.GONE);
listview_layout.setVisibility(View.VISIBLE);
show = true;
}
}
else if (v == back_comments_button)
{ finish(); }
}
I need to place two ImageButtons on top of each other something like the following image.
While placing them has been no issue, I am unable to click the blue button. The red button when clicked works perfectly well.
The XML layout code is as follows:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageButton
android:id="#+id/bluebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/bluebutton" />
<ImageButton
android:id="#+id/redbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/redbutton" />
</FrameLayout>
How do i ensure that both buttons can be clicked ?
You can simply use a RelativeLayout to do that. See this I made just now:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton btn = (ImageButton)findViewById(R.id.bluebutton);
ImageButton btn2 = (ImageButton)findViewById(R.id.redbutton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView t = (TextView)findViewById(R.id.txt);
t.setText("Hello!");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView t = (TextView)findViewById(R.id.txt);
t.setText("Hi, how are you?");
}
});
}
it's just an example
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageButton
android:background="#ff040ab2"
android:id="#+id/bluebutton"
android:layout_width="150dp"
android:layout_height="250dp"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"/>
<ImageButton
android:rotation="90"
android:background="#be2222"
android:id="#+id/redbutton"
android:layout_width="150dp"
android:layout_height="250dp"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt"
android:textColor="#ffffff"/>
</RelativeLayout>
Changed the code.
public class MainActivity extends Activity {
ImageButton red,blue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
red = (ImageButton)findViewById(R.id.redbutton);
blue = (ImageButton)findViewById(R.id.bluebutton);
}
public void onRedButtonClicked(final View view) {
Toast.makeText(getApplicationContext(), "Button red is clicked!", Toast.LENGTH_SHORT).show();
}
public void onBlueButtonClicked(final View view) {
Toast.makeText(getApplicationContext(), "Button blue is clicked!", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And the layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/redbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onRedButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/redbutton"
android:visibility="visible"/>
<ImageButton
android:id="#+id/bluebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onBlueButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/bluebutton"
android:visibility="visible"/>
</FrameLayout>
Hope this helps..:)