Android RelativeLayout and OnClickListener - android

I found many many threads and answers on how to make a Relative layout respond to click event. I've implemented my layout according to them. But still OnClickListener is not called. I have also given selector for it and it works well.
my_list_item xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/air.com.jingit.mobile"
android:layout_width="match_parent" android:layout_height="#dimen/actionBarHeight"
android:clickable="true"
android:background="#drawable/my_list_selector">
<com.loopj.android.image.SmartImageView
android:layout_width="#dimen/actionBarHeight"
android:layout_height="#dimen/actionBarHeight"
android:id="#+id/image"
android:scaleType="fitCenter"
android:layout_marginLeft="20dp"
android:clickable="false"
android:focusable="false"/>
<com.uma.mobile.view.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Loading..."
android:id="#+id/userName"
app:typeface="fonts/HelveticaNeue"
app:customStyle="Regular"
android:textSize="20sp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/image"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#000"
android:clickable="false"
android:focusable="false"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#aaa"
android:layout_alignBottom="#+id/image"></FrameLayout>
and this is where I inflate the view:
public MyListItem(final Context context, final Integer retailerId) {
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.my_list_item, this);
image = (SmartImageView) view.findViewById(R.id.image);
userName = (CustomTextView) view.findViewById(R.id.userName);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("INSIDE LIST","CLICK");
}
});
}
Edit 1:
This Relative layout is added to a linear layout which is inside a scrollview, so that it works like a list view. Does this has something to do???
list_view.xml
<?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"
android:background="#eee">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/listContainer" />
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_centerHorizontal="true"
android:src="#drawable/up_indicator"
android:layout_alignBottom="#+id/scrollView" />

Once i had a RelativeLayout not firing onClick event and it was because i had a Button in the middle of it, and the Button's onClick event was being fired, instead of the RelativeLayout one. Solved it implementing a listener for both of them:
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
//Handle onClick
}
};
relativeLayout.setOnClickListener(listener);
button.setOnClickListener(listener);
Hope it helps!

Add OnClickListener in your RelativeLayout java Class

Related

Accessing element from same layout parent

i have a listview with different components (2 linear layouts and a button) , what i want to do is when i click on a button that's inside one of those linear layouts , i want to access a textview that's inside the other linearlayout , is it possible ?
<?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"
android:orientation="vertical">
<ListView
android:id="#+id/listV2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="85dp"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/nom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/textCname"
android:textSize="21dp"
android:textStyle="bold" />
<TextView
android:id="#+id/numero"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#color/textCother"
android:textStyle="italic" />
<TextView
android:id="#+id/ville"
android:layout_width="match_parent"
android:textColor="#color/textCother"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textStyle="italic" />
</LinearLayout>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:visibility="invisible" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
>
<ImageView
android:id="#+id/remove"
android:onClick="contactRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/edit"
android:onClick="contactEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"/>
<ImageView
android:onClick="contactCall"
android:id="#+id/call"
android:layout_marginLeft="22dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
what i want to do is access the value of android:id="#+id/numero"
when i click on one of the image views
You can set OnClickListener interface provided by View class. Something like this.
Implement listener on your activity class
public class SpinnerActivity extends AppCompatActivity implements
View.OnClickListener
Declare class level variables for your views
private TextView tvNumero;
private ImageView ivRemove, ivEdit, ivContactCall;
Find initialise value in onCreate() method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gildi_spinner);
tvNumero = (TextView) findViewById(R.id.numero)
ivRemove = (ImageView) findViewById(R.id.remove);
ivEdit = (ImageView) findViewById(R.id.edit);
ivContactCall = (ImageView) findViewById(R.id.contactCall);
ivRemove.setOnClickListener(this);
ivEdit.setOnClickListener(this);
ivContactCall.setOnClickListener(this);
}
Implemented class from Onclickistener, where you get on click event for your registered views
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.remove:
case R.id.edit:
case R.id.contactCall:
Toast.makeText(this, tvNumero.getText().toString(), Toast.LENGTH_SHORT).show();
break;
}
}
NOTE : tvNumero.getText().toString() gives you the value of your desired TextView.
UPDATE :
Firstly replace your ListView with RecyclerView
Refer RecyclerView Example tutorial.
Secondly to achieve onClick for your listed items
Refer recyclerview-onclick tutorial.
Pass context when you create your adapter, Use that context to get the inflated view.
Adapter adapter = new Adapter(this);
Then in Adpater Class constructor :
public Adapter(Context context){
context.findViewById(R.id.textview);//consider this as numero textview
}
Then access it as per your requirement. Hope it helps!

Implementing listeners for Views from different layout

I have a ViewPager for the user to swipe between two layouts. My Activity's layout is the one containing the viewPager and I have two additional xml the ones that describe the layout of the screens that are to be swiped. My question is how can I implement the listeners of the buttons of these two layouts in my activity, the one containing the the viewPager. I'll also post the code I've written so far:
main:
<?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"
android:background="#drawable/fonto1" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/grammi" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginEnd="17dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/aristerovelos2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/aristerovelos3"
android:layout_marginTop="21dp" />
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
</android.support.v4.view.ViewPager>
</LinearLayout>
<ImageView
android:id="#+id/deksivelos1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/deksivelos3" />
</RelativeLayout>
first layout to be swiped in the viewPager
<?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="60dp"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/galika"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:src="#drawable/gallika" />
<ImageView
android:id="#+id/germanika"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:src="#drawable/germanika" />
<ImageView
android:id="#+id/ellinika"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:src="#drawable/ellinika" />
<ImageView
android:id="#+id/agglika"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:src="#drawable/agglika" />
The second is something like that, won't need posting it.
This is how I've tried setting my listeners but it won't work.
final LayoutInflater factory = getLayoutInflater();
final View panel = factory.inflate(R.layout.first_panel, null);
iv = (ImageView) panel.findViewById(R.id.el);
and then I set its listener which is trivial though it won't work properly
Ok I'm really sorry, I was really absent-minded I re-inflated another layout, not the one that was on the ViewPager that's why i was getting the button events I will post my way of making it work, just in case
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.first_panel;
break;
case 1:
resId = R.layout.second_panel;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) container).addView(view, 0);
if(resId==R.layout.first_panel)
ibellinika = (ImageView) view.findViewById(R.id.ellinika);
ibellinika.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(false);
progressBar.setMessage("Transfering Data ...");
}
});
return view;
}

Tap preview thumbnail to view high resolution image

I am a new android developer and I am trying to create an android app for android in which menu items will be displayed along with description cost and image.
I have implemented List activity to display the items in a list. I made an image Button which will show the image in a thumbnail. I want to be able to view a high resolution image of the thumbnail when I tap on the image button. I have implemented an onClickListener to listen to the tap event but I do not know the methods that will make the image zoom out on tap.
I have followed this link
but it is quite ambiguous to understand. The Animator class doesn't exist. Can someone guide me out o fthis?
I found the solution after searching for almost a week.
I used PopupWindow Class for the purpose
Following is the Java Code.
PopupWin.java
public class PopupWin extends Activity{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button bPopup = (Button)findViewById(R.id.openpopup);
bPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
Button bDismiss = (Button)popupView.findViewById(R.id.dismiss);
bDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(bPopup, 50, -30);
}});
}
}
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/dim_back" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dim_back"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:src="#drawable/appetimg01" />
<Button
android:id="#+id/dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20sp"
android:text="Go Back"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/appetizerDes1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="0.40"
android:text="Tap the Image to view High Resolution Image."/>
<Button
android:id="#+id/openpopup"
android:layout_width="80sp"
android:layout_height="80sp"
android:layout_gravity="right"
android:layout_margin="40sp"
android:background="#drawable/custombutton"
android:scaleType="centerCrop" />
</LinearLayout>
</LinearLayout>
The background was appearing white but I wanted it transparent for which I created back_dim.xml in drawables and set it along with android:background="#drawable/back_dim".

Android: Listview's row height changes when clicking on the checkbox it contains

I got an activity that contains a single listview.
This listview displays a relativeLayout with a bitmap and a checkbox and when no bitmap is available, a text is displayed.
My problem is with rows that contains no bitmap. When I click on the checkbox, the height of the row changes (increase when checkbox is checked, de crease when unchecked)
activity.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" >
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#drawable/gradient_divider"
android:dividerHeight="1dp" />
</LinearLayout>
row.xml
<?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"
android:orientation="horizontal" >
<ImageView
android:id="#+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:layout_toLeftOf="#+id/registrationCheckBox"
android:filter="true" >
</ImageView>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:paddingLeft="5dp"
android:layout_toLeftOf="#+id/registrationCheckBox" >
</TextView>
<CheckBox
android:id="#+id/registrationCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:paddingRight="5dp" >
</CheckBox>
</RelativeLayout>
My activity code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
hideActionBar();
// init the controls
initControls();
// populate the listView
this.adapter = new RegistrationAdapter(this, refreshContent());
this.listView.setAdapter(this.adapter);
this.listView.setItemsCanFocus(false);
this.listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
this.listView.setOnItemClickListener(
new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
...
}
}
);
}
Any idea on how to prevent my row height to change when clicking on the checkbox ?
I think you are missing the important part of this code, and that is the Adapter code.
How are you handling switching between bitmap and text?
Perhaps set the ImageView to a fixed height (tall enough to fit in any actual image), or provide a dummy invisible image when there is no real image to show.

problem in TextView in android

hi all i m making a sample app. for update List.
i have a list class and its xml is like.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent"></ListView>
<TextView android:id="#+id/more"
android:layout_height="wrap_content"
android:text="Click to view more..."
android:textStyle="normal|bold" android:textColor="#FF8000" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent">
</TextView>
</LinearLayout>
now i have set TouchListener of text View and added the following code in it
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId())
{
case R.id.more:
//update List Method Call..
more.setText("Click to view More..");
adapter.notifyDataSetChanged();
}
// TODO Auto-generated method stub
return false;
}
You see on the 3rd line of switch statement i have added
more.setText("Click to view More..");
line but when my list is updated . The text View is no longer shows in the bottom .
Please Guide my why this is happening to me and whats the solution??
you can use list footer in that case.
add this code in ur java file. and add footer dynamically in your list.
TextView more = new TextView(this);
more.setText("Click to view more...");
more.setClickable(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, Height);
more.setLayoutParams(params);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//update List Method Call..
more.setText("Click to view More..");
adapter.notifyDataSetChanged();
}
});
ListView listView = (ListView) findViewById(R.id.ListView01);
listView.addFooterView(more);
this can help you.
try this ...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:layout_height="fill_parent" android:layout_above="#+id/more"></ListView>
<TextView android:id="#+id/more"
android:layout_height="wrap_content"
android:text="Click to view more..."
android:textStyle="normal|bold" android:textColor="#FF8000" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent" android:layout_alignParentBottom="true">
</TextView>
</RelativeLayout>
try android:layout_height="fill_parent" android:layout_above="#+id/more" in the ListView. Now even if ur ListView grows it wont hide the TextView.
UPDATE
<RelativeLayout android:id="#+id/relativeList"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/LinearBottom3">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/chathlist"
/>
</RelativeLayout>
<RelativeLayout android:id="#+id/LinearBottom3"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content"><!--Make it as center-->
<TextView android:id="#+id/more" android:layout_height="wrap_content"
android:text="Click to view more..." android:textStyle="normal|bold" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent"/>
</ReletiveLayout>
try this..
footer.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content"
android:textSize="30dip" android:gravity="center_horizontal"
android:text="Click to view More.." android:layout_width="fill_parent"
android:id="#+id/more"></TextView>
</LinearLayout>
in class file
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout footer = (LinearLayout)inflater.inflate(
R.layout.footer, null);
TextView more= (TextView)footer.findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//List Update Code
}
});
use the following code..
footer_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:text="footer_text_1"
android:id="#+id/footer_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip">
</TextView>
</LinearLayout>
</LinearLayout>
and use the following in code:
public class listactivty extends ListActivity{
private Context context = null;
private ListView list = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView)findViewById(android.R.id.list);
//code to set adapter to populate list
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
list.addFooterView(footerView);
}
use seperate XML file for ListView..
Thanks
Shahjahan Ahmad

Categories

Resources