Making a LinearLayout act like an Button
I ran into this problem just now. You'll have to set the LinearLayout to clickable. You can either do this in the XML with
android:clickable="true"
Or in code with
yourLinearLayout.setClickable(true);
How to Make LinearLayout Clickable
try this.. hope it works..
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#ff0000" />//ur onclick desired color
<item android:state_focused="false"
android:drawable="@android:color/transparent" />focused color
</selector>
just create new xml and add this code in drawable and set ur linear layoyut backgrout as its name like android:background="@drawable/createdxmlname"
<LinearLayout
android:id="@+id/optionlist"
android:layout_below="@id/divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/createdxmlname"
android:clickable="true">
Clickable LinearLayout in Android
If you look at your code.
You are calling
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trainers);
after the linearlayout binding with click listener.
move these as the first line of the OnCreate
Put buttons at bottom of screen with LinearLayout?
You need to ensure four things:
- Your outside
LinearLayout
haslayout_height="match_parent"
- Your inside
LinearLayout
haslayout_weight="1"
andlayout_height="0dp"
- Your
TextView
haslayout_weight="0"
- You've set the gravity properly on your inner
LinearLayout: android:gravity="center|bottom"
Notice that fill_parent
does not mean "take up all available space". However, if you use layout_height="0dp"
with layout_weight="1"
, then a view will take up all available space (Can't get proper layout with "fill_parent").
Here is some code I quickly wrote up that uses two LinearLayouts in a similar fashion to your code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/db1_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/cow"
android:layout_weight="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="1" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="2" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="3" />
</LinearLayout>
</LinearLayout>
The result looks like similar to this:
How to make LinearLayouts clickable
After trying and failing for hundred times I found the solution to my problem!
Instead of:
ly2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
ly2.setTitle("Hello world!");
return false;
}
});
I should have written:
this.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
setTitle("Hello World");
return true;
}
});
And I shouldn't have used android:clickable
and android:longClickable
for inflated XML(ali_layout.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="68dp"
android:layout_marginLeft="40dp"
android:background="#E0D641"
android:orientation="vertical" >
Finally, the problem solved ...
Related Topics
Notifydatasetchanged Not Working on Recyclerview
How to Find the Android Version Name Programmatically
Implement Double Click for Button in Android
Bufferoverflowexception When Building Application
Android - Detecting Application Launch from Home or History
Android Home Screen Widget (Icon, Label - Style)
Recycle-View Inflating Different Row :- Getting Exception While Binding the Data
Http Post Method Passing Null Values to the Server
How to Implement a Gcm Hello World for Android Using Android Studio
Android: How to Get Current Opened Application Name on Screen
Android.Os.Networkonmainthreadexception . Need to Use Async Task
How to Change App Name Per Gradle Build Type
How to Retrieve More Than 1000 Rows from Parse.Com
Getting Height of Text View Before Rendering to Layout
Garbage Collection Causes:Mediaplayer Finalized Without Being Released