how to make Relative layout onclick work? - android

I have a Relative layout as:
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/i_pay"
android:textAllCaps="false"
android:textColor="#ffffff" />
</RelativeLayout>
I want my relative layout to perform onCLickListener but does not work.
I am able to do the setonclicklistener but it works only with the textview part and not for the image.
btniPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//for ipay
}
});
This works for the text part only but not for the image. Any idea what I might be missing.

Alternatively, you can add android:clickable="false" in your button xml attribute.
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/i_pay"
android:textAllCaps="false"
android:clickable="false"
android:textColor="#ffffff" />
</RelativeLayout>

may be you can find some good example thats use with imageview and buttons but the easy way i did it by using adding TextView instead of images and buttons.
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:clickable="true"
android:gravity="right">
<TextView
android:id="#+id/englishheading1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/urduheading1"
android:text="English text"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="#+id/urduheading1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:text="urdu text1"
android:textColor="#ffffff"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="8dp"
android:background="#ffffff" />
</RelativeLayout>
Here is java code to perform onClick.
RelativeLayout layout1 = (RelativeLayout) view.findViewById(R.id.layout1);
layout1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
}
});

see my code easily run change your code:
.xml file
<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="${relativePackage}.${activityClass}" >
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:id="#+id/btn"
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/ic_launcher"
android:textAllCaps="false"
android:textColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
.java file
public class MainActivity extends Activity {
TextView tv;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
}
});
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_SHORT).show();
}
});
}
}

Just mark android:clickable="true" in your RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_id"
android:clickable="true">
Using ButterKnife lib make bind here:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xml_name);
ButterKnife.bind(this);
}
#OnClick(R.id.activity_id)
void onActivityClick() {
// do smth ...
}

Related

Android click listener on View work in strange manner

I have the following layout.xml containing one ScrollView with some elements(picture and text)
<?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="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/clickable"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/crown" />
<TextView
android:id="#+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/medal" />
<TextView
android:id="#+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/star" />
<TextView
android:id="#+id/n3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity{
LayoutInflater inflater;
RelativeLayout MyLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater = LayoutInflater.from(this);
MyLayout = (RelativeLayout) inflater.inflate(R.id.layout, null, false);
MyLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
Click Listener above does not work when I click screen!!!
But if I change code to this - everything is working
MyLayout.findViewById(R.id.clickable).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
So why should I set click listener to LinearLayout inside ScrollView inside parent RelativeLayout?
Why click listener does not work for root xml element RelativeLayout???
The problem is that your ScrollView is intercepting the touch events and not propagating them to parent layouts (in this case your RelativeLayout).
This thread might have some useful information about this topic.
Just add android:clickable="false" in your ScrollView. Then change your MainActivity like below:
public class MainActivity extends Activity {
LinearLayout myLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLayout = (LinearLayout) findViewById(R.id.layout);
myLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
}
}
Note: activity_main.xml is xml file which you are loading.
Now your OnClick will work as expected.

Click TextView inside linear layout inside ScrollLayout

this below code for my login_activity xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackgroundLogin"
android:clipToPadding="false"
android:fillViewport="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear_layout"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingTop="20dp"
android:paddingBottom="20dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="#drawable/login"
tools:ignore="ContentDescription"/>
</RelativeLayout>
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email_edit_text"
android:hint="#string/email_address_string" />
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:id="#+id/password_edit_text"
android:hint="#string/password_string" />
<Button
android:layout_width="match_parent"
android:textAllCaps="true"
android:text="#string/login_string"
android:background="#drawable/button_style"
android:layout_marginTop="20dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:id="#+id/login_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
</LinearLayout>
</ScrollView>
I want to click on TextView id=need_an_account that like below:
public class LoginActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
}
Note that i don't want to use findViewById() method
But onClick not Working at all how can I solve this problem?
If you don't want to use OnClickListener, Here is an alternative -
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:onClick="myTextViewMethod"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
Now write myTextViewMethodin you Activity
public void myTextViewMethod(View v) {
// Do your stuff here...
}
Plus point, No need to implement OnClickListener, Android will do it in background.
And if you wanna use an Interface View.OnClickListener
Simple, set need_an_account.setOnClickListener(this);
In oncreate add this,
TextView need_an_account = (TextView) findViewById(R.id.need_an_account);
need_an_account.setOnClickListener(this);
You can add OnClick to your xml to avoid using findViewById:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:clickable="true"
android:textStyle="italic"
android:onClick="textViewClick"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
and then write your function in the activity as:
public void textViewClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
no need to implement an OnClickListener if you dont want to define the TextView programatically. Remember to set TextView to clickable and for authenticity i also set background to ?selectableItemBackground so the user knows that the TextView can be clicked

Buttons Overlap

I made a stopwatch using chronometer with four buttons but when i use the visibility modes to make stop and pause button appear they overlap.... Pls explain why...Below is the code.....
Assume the layout file with buttons in relative layout...
public class StopWatchFragment extends Fragment {
Chronometer chronometer;
Button startStopWatch;
Button stopStopWatch;
Button resetStopWatch;
Button pauseStopWatch;
Button resumeStopWatch;
private long lastPause;
//RelativeLayout relativeLayout;
private int check = 0;
public StopWatchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.stopwatch_layout,container,false);
chronometer = (Chronometer) rootView.findViewById(R.id.stopwatch);
startStopWatch = (Button) rootView.findViewById(R.id.startStopWatch);
stopStopWatch = (Button) rootView.findViewById(R.id.stopStopWatch);
resetStopWatch = (Button) rootView.findViewById(R.id.resetStopWatch);
pauseStopWatch = (Button) rootView.findViewById(R.id.pauseStopWatch);
resumeStopWatch = (Button) rootView.findViewById(R.id.resumeStopWatch);
relativeLayout = (RelativeLayout) rootView.findViewById(R.id.parentRelativeLayout);
pauseStopWatch.setVisibility(View.GONE);
//final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(relativeLayout.getLayoutParams());
startStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
startStopWatch.setVisibility(View.GONE);
pauseStopWatch.setVisibility(View.VISIBLE);
if(check == 1){
resumeStopWatch.setClickable(true);
}
else{
resumeStopWatch.setClickable(false);
}
//params.setMargins(16,16,16,16);
//pauseStopWatch.setLayoutParams(params);
}
});
pauseStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
check = 1;
lastPause = SystemClock.elapsedRealtime();
chronometer.stop();
}
});
resumeStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
chronometer.start();
resumeStopWatch.setClickable(false);
}
});
stopStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.stop();
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
resumeStopWatch.setClickable(false);
}
});
resetStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.stop();
resumeStopWatch.setClickable(false);
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
}
});
return rootView;
}
}
This is the layout file pls refer for this....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
This is the way buttons work when click on start button
You are hiding the button, but you are still listening with the OnClickListener. Try adding:
pauseStopWatch.setOnClickListener(null);
Then, when you make your button visible:
pauseStopWatch.setOnClickListener(whatever);
Try this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:visibility="gone"
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</LinearLayout>
</LinearLayout>
In the above, the pause button will be set to Gone. You make it visible later when user clicks start button

Don't work onClick xml ? Android

I have an xml that handle onclick in xml the onClick of Button working but onClick TextView don't work.
Bellow my row_listview.xml :
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#ffffff"
android:gravity="center"
android:orientation="horizontal" >
<HorizontalScrollView
android:id="#+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/row_cell_text_dummy_multilevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:singleLine="false"
android:textColor="#ffffff"
android:textSize="10dp" />
<Button
android:id="#+id/row_cell_btn_multilevel"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:onClick="CellButtonClick" />
<TextView
android:id="#+id/row_cell_text_multilevel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="7dp"
android:layout_weight="1"
android:gravity="center|left"
android:clickable="true"
android:onClick="CellTextClick"
android:singleLine="true"
android:textColor="#000000"
android:textSize="10dp" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
And bellow is my StartActivity.class :
public class StartActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
public void CellTextClick(View v){
try {
Log.i("test", "OK");
} catch (Exception e) {
}
}
}
Notice : my android:onClick="CellButtonClick" work good but my android:onClick="CellTextClick" don't work .
try to TextView
android:clickable="true"
I guess you have an adapter and in your adapter you have a onclickListener for your TextView(android:id="#+id/row_cell_text_multilevel") if you have ! you should comment it then use from android:onClick="CellButtonClick".
Add this to textview android:clickable="true"
Have you tried setting android:clickable = "true" under TextView ?
Add this code in xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
and the following is the java code
public class test extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
TextView tv= (TextView)findViewById(R.id.textView1);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(test.this, "Textview Clicked", Toast.LENGTH_LONG).show();
}
});
}
}
it working

How to place 2 buttons at the same position in my layout

I have My Table row like below in my xml layout:
<TableRow
android:layout_marginTop="10dp"
>
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="256dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/titlename"
android:layout_="#+id/playbtn"
android:layout_marginTop="4dp"
/>
<Button
android:id="#+id/playbtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#drawable/play"
android:layout_marginBottom="3dp"/>
<Button
android:id="#+id/pausebtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_toTopOf="#+id/playbtn"
android:layout_alignParentBottom="true"
android:background="#drawable/pause"
android:layout_marginBottom="3dp"/>
</TableRow>
and my output is as below,
my requirement is to show play pause buttons at the same position in my layout?
Could any one help?
Use FrameLayout and put one button over another
Like this
<FrameLayout ... >
<Button
android:id="#+id/playbtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/play"/>
<Button
android:id="#+id/pausebtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/pause"/>
</FrameLayout>
This will put Pause button over Play Button. Make the necessary button visible and invisible according to your need
Try using this layout. Use combination of LinearLayout and FrameLayout to achieve the desired result.
<TableRow
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weight="1"
android:layout_alignLeft="#+id/titlename"
android:layout_="#+id/playbtn"
android:layout_marginTop="4dp"
/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/playbtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/play"/>
<Button
android:id="#+id/pausebtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/pause"/>
</FrameLayout>
</LinearLayout>
</TableRow>
once Try this:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8" />
<Button
android:id="#+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".2"
/>
</TableRow>
Main Activity
private boolean playing = false;
Button play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play=(Button) findViewById(R.id.play);
play.setBackgroundResource(R.drawable.pause);
play.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(playing){
playing = false;
play.setBackgroundResource(R.drawable.pause);
}
else {
playing = true;
play.setBackgroundResource(R.drawable.play);
}
}
});
}
Why can't you try merge?
Have a glance of this.
http://android-developers.blogspot.in/2009/03/android-layout-tricks-3-optimize-by.html
You don't need to create two buttons for it. You can do it with single. I have created a sample program. I find this to be a cleaner approach.
Layout for MainActivity
<LinearLayout 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:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/buttonPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Here is MainActivity
package in.live.homam.imagebuttonexample;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button buttonPlayPause;
private static final int resourceIdPlay = R.drawable.play;
private static final int resourceIdPause = R.drawable.pause;
private int resourceIdButton = resourceIdPlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPlayPause = (Button) findViewById(R.id.buttonPlayPause);
buttonPlayPause.setBackgroundResource(resourceIdButton);
buttonPlayPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(resourceIdButton == resourceIdPlay) {
resourceIdButton = resourceIdPause;
Log.d("Tag", "Playing");
}
else {
resourceIdButton = resourceIdPlay;
Log.d("Tag", "Paused");
}
buttonPlayPause.setBackgroundResource(resourceIdButton);
}
});
}
}

Categories

Resources