I have two ImageViews, one text field and one WebView in a LinearLayout. My ImageView listener is not working. Here is my 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:background="#drawable/bg_main"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="back"
android:adjustViewBounds="true"
android:clickable="true"
android:src="#drawable/back_icon" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:clickable="true"
android:contentDescription="forward"
android:src="#drawable/back_icon"/>
<EditText
android:id="#+id/adressBar"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ems="10" >
</EditText>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here is my Java code. I should point out that the Log.d statement is not firing, so it is definitely not getting called:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState == null) {
L.d(">>onCreateView", "savedInstanceState null");
}
View v = inflater.inflate(R.layout.browser,null);
mWebView = (WebView) v.findViewById(R.id.webView);
back =(ImageView) v.findViewById(R.id.imageView1);
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("backkkkkkkkkkkkkkkk", "msg");
}
});
return v;
}
try this
v = inflater.inflate(R.layout.browser, container, false);
ImageView back = (ImageView)findViewById(R.id.imageVIew1);
back.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Log.d("backkkkkkkkkkkkkkkk", "msg");
}
});
try this-
back.setOnClickListener(this);
and implement onclicklistener..
You need to inflate the view like
v = inflater.inflate(R.layout.browser, container, false);
back =(ImageView) v.findViewById(R.id.imageView1);
back.setOnClickListener(new View.OnClickListener())
{
..
}
set to focusable false for all the views in your xml file. or setOnClickListener for imageview in onActivityCreated() method in your fragment.
I had a similar issue and fixed it by adding android:layout_width="44dp" to make sure user would have enough area to click on. Then, you can align image content the way you wannt. In my case:
<ImageView
android:id="#+id/locationImageView"
android:layout_width="44dp"
android:layout_height="16dp"
android:layout_centerVertical="true"
android:layout_marginStart="4dp"
android:layout_toEndOf="#id/locationText"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="#drawable/icn_edit_small_white"/>
Related
I have a structure in my android application
LinearLayout > FrameLayout >ImageView(used as a FrameLayout
Background) & RelativeLayout > TextView & ImageView.
I need that when I click in the FrameLayout or RelativeLayout or FirstImageView ( because it is the Bigger Zone , the textView or 2nd Image view are small ) do an action.
I tried to add setOnClickListener to this item but didn't work.
This is my Code:
public class HomeFragment extends Fragment {
View rootView;
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home_fragment, container, false);
RelativeLayout paleoFoodBackGround = (RelativeLayout) rootView.findViewById(R.id.relativeLytPaleoFoodHome);
paleoFoodBackGround.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("1FOOD_LIST", "onClick: ");
}
});
return rootView;
}
public void clickFood(){
Log.d("2FOOD_LIST", "onClick: ");
}
}
This is my Layout structure:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:orientation="vertical"
android:paddingLeft="2.5dp"
android:paddingRight="5dp"
android:weightSum="100">
<FrameLayout
android:id="#+id/framePaleoFoodHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:paddingBottom="2.5dp">
<ImageView
android:id="#+id/imgViewPaleoFoodBackGround"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="center"
app:srcCompat="#drawable/recipes" />
<RelativeLayout
android:id="#+id/relativeLytPaleoFoodHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<ImageView
android:id="#+id/imgViewRecipesIconHome"
style="#style/iconHome"
app:srcCompat="#drawable/cooking"
/>
<TextView
android:id="#+id/txtViewRecipesTxtHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/textViewHomeSquare"
android:text="#string/RecipesHome"
android:layout_below="#id/imgViewRecipesIconHome"/>
</RelativeLayout>
</FrameLayout>
<Framelayout> .... </Framelayout>
</LinearLayout>
I recommend using ImageButton instead of image. Also set your RelativeLayout to have android:clickable="true". Also set all children views that are not clickable with android:clickable="false"
I have a child view and a root view. Im trying to add onClick listener to child view but the onClickListener is never getting fired.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.notification, container, false);
linear = (LinearLayout) rootView.findViewById(R.id.linear);
for (int i = 0; i <= 10; i++) {
childView = inflater
.inflate(R.layout.notification_fragment, linear);
TextView txt = (TextView) childView.findViewById(R.id.textView1);
txt.setText("welcome");
txt.setId(i);
}
childView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplicationContext(),""+mChildView.getId(),Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
notification_fragment.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:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="PI 12343"
android:id="#+id/textView1"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="8dp"/>
</LinearLayout>
notification.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" >
<com.stack.widgets.SegmentedButton
android:id="#+id/segment_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_margin="5dip"
android:checkedButton="#+id/button_one"
android:background="#3dd2c6"
android:orientation="horizontal" >
<RadioButton
android:id="#id/button_one"
android:button="#null"
android:gravity="center"
android:minHeight="33dip"
android:minWidth="40dip"
android:text="Pending"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#01A9DB" />
<RadioButton
android:id="#+id/button_two"
android:button="#null"
android:gravity="center"
android:minHeight="33dip"
android:minWidth="40dip"
android:text="Completed"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#01A9DB" />
</com.stack.widgets.SegmentedButton>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true"
android:id="#+id/linear"></LinearLayout>
</LinearLayout>
When the pending radio button is pressed, the childviews will be inflated upon rootview. Childviews should be clickable.
set your listener inside for loop, currently it is setting listener for your last view only.
for (int i = 0; i <= 10; i++) {
childView = inflater
.inflate(R.layout.notification_fragment, linear);
TextView txt = (TextView) childView.findViewById(R.id.textView1);
txt.setText("welcome");
txt.setId(i);
childView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplicationContext(),""+mChildView.getId(),Toast.LENGTH_SHORT).show();
}
});
}
My ListView gets filles by this
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="#+id/projectImage"
android:maxHeight="70dp"
android:maxWidth="70dp"
android:adjustViewBounds="true"
android:minHeight="50dp"
android:minWidth="50dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/projectTitle"
android:textStyle="bold"
android:focusable="false"
android:clickable="false"
android:textSize="15dp"
android:text="projecttitle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/projectTopic"
android:focusable="false"
android:clickable="false"
android:textSize="14sp"
android:text="projectTopic"
android:layout_marginTop="3dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/projectdeadline"
android:textSize="14sp"
android:text="projectdeadline"
android:layout_marginTop="3dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="projectTaskCount"
android:id="#+id/projectTaskCount"
android:layout_marginTop="3dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
It contains one ImageView and three TextViews.
If I click on the ImageView I want to start my Activity Example1, else open next Activity WiFi.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent in = new Intent(this, WiFi.class);
in.putExtra("projectFileNamesMap", projectFileNamesMap.get(position));
in.putExtra("position", position);
in.putExtra("sessionId", sessionId);
startActivity(in);
}
How can I solve my Problem?
Summarized:
If I click on ImageView open Example1 Activity.
If elsewhere clicked, then open WiFi Activity.
Hope everybody can understand this.
Kind Regards!
use image.setOnclickListener and view.setOnItemCLickListener inside getView method to perform click on image view and whole view.
First set your image:
android:clickable="true" //setOnClickListener makes a view clickable if it doesn't have that as a default but use it anyway.
then:
imgView.setOnClickListener(new View.OnClickListener() {
//#Override
public void onClick(View v) {
Log.v(TAG, " click");
}
});
pls tell me if this works
Write OnClickListener for ImageView when creating a new Instance for each ImageView elements for the listView in the Adapter's getView method
Also make sure before you addView's to the Linearlayout you set appropriate LayoutParams.
public View getView(final int position, View convertView, ViewGroup parent) {
TextView txtOne, txtTwo, txtThree;
ImageView iv;
LinearLayout layout;
if(convertView == null){
layout = new LinearLayout(context);
txtOne = new TextView(context);
txtTwo = new TextView(context);
txtThree = new TextView(context);
iv = new ImageView(context);
} else{
layout = (LinearLayout) convertView;
txtOne = (TextView) layout.getChildAt(0);
txtTwo = (TextView) layout.getChildAt(0);
txtThree = (TextView) layout.getChildAt(0);
iv = (ImageView) layout.getChildAt(0);
}
layout.addView(txtOne);
layout.addView(txtTwo);
layout.addView(txtThree);
layout.addView(iv);
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your handling here
}
});
}
I have a viewpager with a fragment that contains a button. Here is the onCreateView of the fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment, container, false);
refreshButton = (Button) v.findViewById(R.id.refreshButton);
refreshButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
refreshButton_Click();
}
});
return v;
}
I can verify that refreshButton_Click is not being called when I click on the refreshButton (which is clearly visible). I am guessing that the fragment just isn't getting focus or that it isn't allowing child elements to have focus.
Here is the layout for the fragment:
<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"
tools:context="com.me.philip.firehydrant.FeedFragment"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffededed">
<TextView
android:id="#+id/headerTextView"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="header"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:layout_margin="2dp"
android:text="refresh"
android:id="#+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"/>
</LinearLayout>
<ExpandableListView
android:id="#+id/postList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></ExpandableListView>
</LinearLayout>
EDIT: I forgot to mention: the button doesn't do the usual blue lighting up that buttons do when they are clicked, so I'm pretty sure it isn't the setOnClickListener that isn't working, but rather that the button is unable to get focus
Well, I believe I solved it. I had the ViewPager nested inside a DrawerLayout. I guess the similarity of their swiping gestures make them unworkable together.
any ideas why my ImageButton isn't working in my Fragment?
The ImageButton contains a Drawable and is transparent.
When I place it in my RelativeLayout below, its working...
<RelativeLayout
android:id="#+id/rl_newsdet_root"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/bg"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_newsdet_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:layout_marginTop="7dp"
android:textSize="20sp"
android:layout_centerHorizontal="true"
/>
<ImageButton
android:id="#+id/btn_newsdet_back"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="#drawable/bt_back"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
android:background="#color/white"
android:gravity="left"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_newsdet_name"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:textStyle="bold"
android:textColor="#color/cyan"
android:background="#color/grey"
android:paddingLeft="1dp"
android:paddingRight="1dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_newsdet_time"
android:textColor="#color/white"
android:textSize="18sp"
android:layout_toRightOf="#+id/tv_newsdet_name"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:background="#color/grey"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/tv_newsdet_text"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_below="#+id/tv_newsdet_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
My onClickListener with AndroidAnnotations
#ViewById
ImageButton btn_newsdet_back;
#Click(R.id.btn_newsdet_back)
void back() {
Log.i(TAG,"back");
mActivity.popFragments();
}
EDIT
onClick is working (called), when its below the TextView. Can anybody explain why?
If, as you said, everything works when you put the button inside the RelativeLayout below the reason should be that the second RelativeLayout has its parameters declared as:
android:layout_width="match_parent"
android:layout_height="match_parent"
Which means it is "filling" its parent, that is the original RelativeLayout containing the button. It's like you're creating a layer that covers the button and keeps any interaction with it from working. When you click on the button, you're actually clicking on the second RelativeLayout.
I didn't actually tried this, but you should consider setting:
android:layout_below="#id/btn_newsdet_back"
On the RelativeLayout below the button.
You can try this code:
This is code of Fragment: TestFragment.java
public class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_one, container, false);
ImageButton button = (ImageButton) view.findViewById(R.id.img2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "Click on fragment ",
Toast.LENGTH_LONG).show();
}
});
return view;
}
}
This is layout of 'TestFragment`
<?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" >
<ImageButton
android:id="#+id/img2"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
This is code of MainActivity.java
public class MainActivity extends FragmentActivity {
ImageButton imgButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
FragmentManager fragmentManager = getSupportFragmentManager();
TestFragment fragment = (TestFragment) fragmentManager
.findFragmentById(R.id.fragment);
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
// fragmentTransaction.add(fragment, "");
fragmentTransaction.commit();
}
}
This is layout of MainActivity
<?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" >
<fragment
android:id="#+id/fragment"
android:name="com.alarmdemo.TestFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Here ImageButton is placed in a Fragment and it is working fine.
No need to add clickable in image button .Try following:
<ImageButton
android:id="#+id/btn_newsdet_back"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mymethod"
android:src="#drawable/bt_back"
/>
add in java
public void mymethod(View v)
{
//your method
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sample,
container, false);
addListenerOnButton();
}
private void addListenerOnButton() {
imageButton = (ImageButton) rootview.findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(MainActivity.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
I solved it.
The problem was I was using a transparent Actionbar which was the overlay I was looking for.
The following line in my onCreate() solved this:
getActivity().getActionBar().hide();