I have a RelativeLayout with a few TextView as children
<RelativeLayout
android:id="#+id/shift_parent_name"
android:layout_width="fill_parent"
android:layout_weight="0.25"
>
<TextView
android:id="#+id/shift_parent_nametitle"
android:text="#string/shift_parent_nametitle"
style="#style/header_text"
/>
<TextView
android:id="#+id/shift_parent_namefield"
android:layout_alignParentRight="true"
android:layout_below="#id/shift_parent_nametitle"
style="#style/wrap"
/>
How do I go about using the RelativeLayout as a button to react to a click event if any part of the area is pressed?
Just add a OnClickListener to your RelativeLayout
I have a RelativeLayout called "RelativeMain1". This is how i make it start Activity
RelativeLayout relativeclic1 =(RelativeLayout)findViewById(R.id.RelativeMain1);
relativeclic1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
startActivityForResult(new Intent(A_My_Galaxy.this,C_Student_Book_Planet.class), 0);
}
});
After you add the onClickListener to your layout, it should work.
Add a "OnClickListener" to your RelativeLayout.
Note: Don't forget to add android:clickable="true" to your RelativeLayout.
Give your RelativeLayout an ID, like you typed shift_parent_name
Set your RelativeLayout XML to android:clickable="true"
Your final xml will be look like this:
<RelativeLayout
android:id="#+id/shift_parent_name"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:clickable="true">
then add the code in your onCreate method :
RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.shift_parent_name);
relative1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
startActivity(new Intent(Main.this, About.class) );
}
});
Make sure to change your activity names, Main.this and About.class.
The main activity called Main.java and the second is About.java
RelativeLayout rl=(RelativeLayout)findViewById(R.id.RelativeMain1);
relativeclic1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
You can simply just add this android:onClick="onClick"
Related
I have a relative layout that I want to make tappable, including its child views.
<RelativeLayout
android:id="#+id/houses_rel_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#drawable/white_grey_border_bottom">
<HorizontalScrollView
android:id="#+id/houses_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
...
</LinearLayout>
</HorizontalScrollView>
<TextView... />
.....
</RelativeLayout>
I want to set an onclicklistener on the relative layout, the horizontal view, and the textviews, and a separate onclicklistener on the icon.
#BindView(R.id.houses_rel_1)
ViewGroup mHouse;
#BindView(R.id.fav_icn_1)
ImageView mFavIcn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_house_list);
ButterKnife.bind(this);
mHouse.setOnClickListener(new ViewGroup.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext, SomeActivity.class);
mContext.startActivity(intent);
}
});
mFavIcn.setOnClickListener(new View.OnClickListener() {
...
});
}
Currently, the new activity is staring only when I tap on the textviews, but nothing happens when I tap on the horizontalscrollview. Do I have to duplicate the onclicklistener to horizontalscrollview?
Please use setOnTouchListener() on horizontalScrollView instead of setOnClickListener().
Mmmmmm...The reason is very complicated. After I read the source code of HorizontalScrollView.java, I found it overrides these methods of View or ViewGroup--onTouchEvent, onInterceptTouchEvent.Without use super.onTouchEvent,super.onInterceptTouchEvent.It partly means if you set a OnClickListener on it(HorizontalScrollView), the onClick event will never be invoked.
If you want to know more about, you can read the source code too. It will more helpful then I telling you the reason, cuz I may have some misinterpretations in reading.
I hope that helps you.
Try grouped onClick of Butter Knife :
#OnClick({ R.id.houses_1, R.id.houses_rel_1 })
public void startSomeActivity() {
Intent intent = new Intent(mContext, SomeActivity.class);
mContext.startActivity(intent);
}
I have set the onClick property of an EditText which is located in a fragment:
<EditText android:id="#+id/edittext1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:inputType="none" android:maxLines="1"
android:singleLine="true" android:layout_marginTop="16dp"
android:focusable="false"
android:longClickable="false"
android:clickable="true"
android:onClick="doSomething"
android:cursorVisible="false"
android:editable="false">
Then in the fragment class I have:
public void doSomething(View view) {
//show dialogfragment...
}
But the method doSomething is grayed out and I get the warning 'Method doSomething is never used'.
Note: This code was originally in an activity and was working fine.
Is there another way to handle onClick in fragments?
first initialize an EditText instance in the top of your fragment
EditText et;
then
in your onCreateView()
add:
etEmployee=(EditText)rootView.findViewById(R.id.edittext1);
then implement your onClickListener()
et.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//what ever you need to do goes here
}
});
Note: in fragments you have to refer to the inflatedView to be able to access your editText in this case rootView.
also the code you are using doesn't work becouse here you are using fragments and using onClick attribute in your xml would only make you able to use it in the MainActivity that contain your fragment.
hope this will help.
If you add android:onClick="doSomething" to your fragment then method will be invoked in your activity but not in Activity. If you want the callback in your fragment add through pragmatically.
edittext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
});
You can always try to add a OnClickListener to the EditText if it does not matter to you in which way the function gets called.
edittext1 = (EditText) view.findViewById(R.id.edittext1);
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doSomething();
}
});
In your layout XML try specifying a tools:context="com.mypackage.path.MyFragment". Don't know if it'll fix it for 'onClick', but it has fixed similar issues for me in the past.
This goes in the top most 'ViewGroup' in your layout file, example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.mypackage.path.MyFragment" />
I'm making a button in xml(res / layout / activity_home.xml), like this:
<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=".HomeActivity" >
<ImageView
android:id="#+id/imageView1"
android:src="#drawable/schkopwide"
android:contentDescription="#string/HTI"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="Intent i = new Intent(activity_store.xml);
startActivity(i);"
android:text="#string/HTI" />
</RelativeLayout>
so what should I add into this xml to let it redirect to another xml page (res / layout / activity_store.xml)?
Thank you
If you Want to show two different layouts in Same Activity, then ViewSwitcher is best layout.
You can add multiple layouts in ViewSwithcher. And Replace them by using viewswitcher.next(); function.
<ViewSwitcher
android:id="#+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- Add Two View’s Here -- >
</ViewSwitcher>
You can take reference from this link: http://abhiandroid.com/ui/viewswitcher
Try out as below:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="start"
android:text="#string/HTI" />
In your main activity :
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
Here is your ActivityStore Class code:
public class ActivityStore extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
}
Also add the activity into your mainfest file.
<activity
android:name=".ActivityStore"
android:label="#string/app_name"/ >
You can't add the launch of an Intent inside the onclick parameter in XML. You have to do it by code.
In your code:
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
And in the OnCreate of the ActivityStore class, put this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
NOTE: I supposed that yous activity_store class is called ActivityStore
You need to check on Android documentation :
Activity
OnClickListener
Intent
http://developer.android.com/training/index.html
Good luck.
Try this,
Statically include XML layouts inside other XML layouts. use include. Add the below code in your activity_store.xml
<include layout="#layout/activity_home"/>
Sure you will get solution.
A simple way would be to create an Activity with another xml attached to it and then use intent.
I want to add text under a image button, which are also clickable, and will be redirected to the same activity as the image button. basically it's like the app list in any android phone, e.g.
If you have your ImageButton declared in XML, then just put it into a LinearLayout which also contains a TextView and set the onClickListener on the LinearLayout. The structure would be like
<LinearLayout
... > <!-- This is the LinearLayout for the xml document -->
<!-- Some other layout code here if you please -->
<LinearLayout
android:id="#+id/linLayout"
... >
<ImageButton
... />
<TextView
... />
</LinearLayout>
</LinearLayout>
And then in your java:
LinearLayout layout = (LinearLayout)findViewById(R.id.linLayout);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
If you're adding each ImageButton dynamically via java code, then it will still maintain the same structure. Let me know if I need to add anything.
setAlpha and do the below
<Button
android:id="#+id/comparePack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:onClick="compareButtonClick"
android:text="#string/compare"
android:drawableTop="#drawable/compare1edit"
android:gravity="left|center_vertical" />
if you want to create this king of list use gridview
and in getview method inflate customlayout with imageview and textview
Also you can set onClickListener to TextView
<!-- Some other layout code here if you please -->
<LinearLayout
android:id="#+id/linLayout"
... >
<ImageButton android:id="#+id/btn1"
... />
<TextView android:id="#+id/tv1"
... />
</LinearLayout>
java code:
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myClick();
}
});
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myClick();
}
});
I try to get the effect click background color for linear layout. I've set clickable to linear layout. and from the code also I've put the click listener the setBackgroundResource.
Here it is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/llinsertmem"
android:clickable="true"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50px">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="PUSH it"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
and the java code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
linearInsertMem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(android.R.drawable.list_selector_background);
Toast.makeText(testdoank.this, "succeded", Toast.LENGTH_SHORT)
.show();
}
});
}
When first time click the clickable linearlayout, the toast text is displayed but the background color click effect doesn't. The flash background click color is only work from the second click.
any idea what the problem is?
Is not necessary do anything in JAVA code.
You can only add this as attribute:
android:background="#android:drawable/list_selector_background"
And it works for me (on Android 2.2 device)
After try and error, somehow it's work.
just put the setBackgroundResource also on the onCreate.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
linearInsertMem.setBackgroundResource(android.R.drawable.list_selector_background);
linearInsertMem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(android.R.drawable.list_selector_background);
Toast.makeText(testdoank.this, "succeded", Toast.LENGTH_SHORT)
.show();
}
});
}
Don't know the logic explanation. if you have a thought, please.
In the onClick method you are using v as the View which is passed, but that view may not be the LinearLayout which you want to change the background.
Thus you need to create a class level variable or a final varibale and pass the handle for the LineraLayout to the onClick method.
Remove/cut from onCreate:
linearInsertMem.setBackgroundResource(android.R.drawable.list_selector_background);
and paste it into onClick or change v. into linearInsertMem.
I think that Exlipse will then demand that linearInsertMem must be final like:
final LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
Or you can define this object above onCreate like this:
LinearLayout linearInsertMem;
then in onCreate you state:
linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
then onClick method will know exactly which view you want to change if you use linearInsertMem.setBackgroundResource...