Button from other layout not respond - android

I´m working on two layouts: the main one and another one (a card layout). I included the card layout inside the main layout, with the following code:
<include
android:id="#+id/miPrueba"
layout="#layout/card_product_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:elevation="10dp"
android:layout_margin="15dp"/>
This works fine, but I have buttons in my main layout and I need to use them. When I try to find them, using findViewByIdid from my card layout, I use this code:
View view = LayoutInflater.from(getApplication()).inflate(R.layout.card_product_details, null);
Button prueba = (Buttton) view.findViewById(R.id.buttonId);
prueba.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Toast por defecto", Toast.LENGTH_SHORT).show();
}
});
What I get is nothing, no error, no null pointer exception, nothing. Simple, it does not respond when I click it.
How I can solve it?

Need more of your code but from what you have given the reason you are having this problem is that the card doesn't have the button buttonId
When you are creating
View view = LayoutInflater.from(getApplication()).inflate(R.layout.card_product_details, null);

Related

I want to display the list of attachments from a single textview using a list. But I'm able to display only one object

I'm making an app where I want to display I want to display all the attachments. But can view only one attachment. Below is the function along with the XML :
//this is the function that displays attachments:
public void showAttachments(List<Attachment> attachments) {
mAttachmentsLayout.setVisibility(View.VISIBLE);
for (Attachment attachment : attachments) {
View view = getLayoutInflater().inflate(R.layout.attachment_item_layout, mAttachmentsLayout, true);
view.setTag(attachment);
//inflating the layout xml file
TextView attachmentTitle = (TextView)view.findViewById(R.id.attachment_title);
String title = attachment.getFileName();
//getFileName() returns the name of the attachment
if (TextUtils.isEmpty(title)) {
title = attachment.getFileName();
}
//assigning title to textview:
attachmentTitle.setText(title);
//want to display all attachments
//not just one
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBasePermissionPresenter.onClickAttachment((Attachment)v.getTag());
}
});
}
}
Attachments container size increases but only one attachment name is shown
This is the corresponding XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/attachmentHeading"
android:padding='#dimen/posting_details_attachment_item_padding'
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:id="#+id/attachment_title"/>
</LinearLayout>
I know the solution is really easy but please help out. Thanks.
You are able to only single attachment because you are inflating the same view every time in this fragment of code:
View view = getLayoutInflater().inflate(R.layout.attachment_item_layout, mAttachmentsLayout, true);
You can get your desired result in two ways:
Create different layout.xml for every Image (This will be a lot of work)
or you can add image at run time
I would go with the second option. Do not set your image in the xml file instead add the image at runtime. The code goes as follows:
ImageView imView=(ImageView)view.findViewById(Your_Image_View_ID_here);
imView.setImageResource(Your_Resource_File_ID_Here);
And change the Resource_Id as the loop increments.
Have fun :)

Cardslib's CardView OnCardClickListener's onClick method not being called

I am using this library in my app: Card Library : Guide and created a card as shown in the example.
Now I need to handle a click event, but I receive an error when instantiating the following: new Card.OnCardClickListener, which says OnCardClickListener is abstract: cannot be instantiated.
In my Activity I have this code:
Card card = new Card(getBaseContext());//getContext()
CardHeader header = new CardHeader(getBaseContext());
card.addCardHeader(header);
final CardViewNative cardView = (CardViewNative) findViewById(R.id.carddemo);
cardView.setCard(card);
cardView.setClickable(true);
cardView.setOnClickListener(new Card.OnCardClickListener()); //error here
What's wrong? How can I set my card as clickable and then handle click/touch events?
EDIT
If I use it this way:
cardView.setOnClickListener(new Card.OnCardClickListener(){//error here
#Override
public void onClick(Card card, View view) {
// This will execute when the the card is clicked
}
});
it says cannot resolve method setOnClickListener(it.gmariotti.cardslib.library.internal.Card.OnCardClickListener)
EDIT1
You guys are right, I set the setOnClickListener on the card itself, and there's no error anymore,
but still it doesn't perform any action when the card is clicked. That's the new code:
//Create a Card
Card card = new Card(this);//getContext()
//Create a CardHeader
CardHeader header = new CardHeader(this);
//....
//Add Header to card
card.addCardHeader(header);
//Set card in the cardView
CardViewNative cardView = (CardViewNative) this.findViewById(R.id.carddemo);
cardView.setCard(card);
card.setClickable(true);
card.setOnClickListener(new Card.OnCardClickListener(){
#Override
public void onClick(Card card, View view) {
// This will execute when the the card is clicked
Toast.makeText(CardMariotti.this,"Clickable card", Toast.LENGTH_LONG).show();
}
});
I performed a debug, but the onClick method is never called. How come?
Card.OnCardClickListener is an Interface. You must create the class that defines how the functions within the interface are to be used. Try this:
card.setOnClickListener(new Card.OnCardClickListener() {
#Override
public void onClick(Card card, View view) {
// This will execute when the the card is clicked
}
});
Source for answer: https://github.com/gabrielemariotti/cardslib/blob/master/doc/CARD.md#clickable-card
Information on Java interfaces: Java Interfaces?
EDIT
Furthermore, remove android:clickable="true" from the RelativeLayout contained by the card. It's stealing the click for itself.
I've found the error but I don't know how to handle it.
It's in the layout containing the card, cardslib_item_card.xml. If I remove everything that I've added and I just keep this:
<it.gmariotti.cardslib.library.view.CardViewNative xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
card:card_layout_resourceID="#layout/native_card_layout"
style="#style/native_recyclerview_card.base"
card_view:cardCornerRadius="4dp"
android:id="#+id/carddemo"
android:layout_marginTop="12dp"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginLeft="12dp" android:layout_marginRight="12dp">
then the card gets clicked, but if try to personalize it, for example adding a Relative Layout, as such:
<it.gmariotti.cardslib.library.view.CardViewNative
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
card:card_layout_resourceID="#layout/native_card_layout"
style="#style/native_recyclerview_card.base"
card_view:cardCornerRadius="4dp"
android:id="#+id/carddemo"
android:layout_marginTop="12dp"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginLeft="12dp" android:layout_marginRight="12dp">
<RelativeLayout android:id="#+id/cardlayout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="?android:selectableItemBackground"
android:clickable="true">
<!-- layout containing 3 TextViews -->
</RelativeLayout>
</it.gmariotti.cardslib.library.view.CardViewNative>
then the card doesn't get clicked anymore.
To be more precise, it gets clicked just sometimes, as if the RelativeLayout is covering the whole card and and when i find a spot where it's not then it detects my finger. It works if I only add an empty card, but how am I supposed to customize it then?
It's like having two views on top of each other
You have to define the clickListener on the card (model) instead of CardView (view)
//Set onClick listener
card.setOnClickListener(new Card.OnCardClickListener() {
#Override
public void onClick(Card card, View view) {
Toast.makeText(getActivity(),"Clickable card", Toast.LENGTH_LONG).show();
}
});
At the same time you should use:
//it is not necessary if you define a ClickListener on the card
card.setClickable(true);
Btw, you should also use something like this in an Activity:
Card card = new Card(this);

Android - Using Buttons With Single Layout

I have created an Activity that uses the ViewFlipper to Switch between Different elements. Each element represents an Item in a store. I would like to add a "Buy" Button to each View. I am however not sure how to do this, since all the views use the default layout i have created. I have added the information like the Price of the Item etc Programmatically. So i am uncertain how to add a listener to the button, since they will all refer to the same button in the xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/credit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="220dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="104dp" />
<TextView
android:id="#+id/credit_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/credit_button"
android:layout_alignTop="#+id/credit_button"
android:layout_marginRight="22dp"
android:layout_marginTop="21dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="normal" />
<TextView
android:id="#+id/credit_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/credit_button"
android:layout_alignRight="#+id/credit_type"
android:layout_centerVertical="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="15dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
As you can see, the Button id is "Credit Button". So now to be able to differentiate between the different store items' buttons what would i have to do?
Note, i am adding store items dynamically as well, so i cant simply create all the views separately using xml.
OK HERE IS THE UPDATED ANSWER. I used everyones responses below to fix teh issue. So Thank you all :)
// PerkView
View PerkView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(PerkView);
Button perkButton = (Button)
PerkView.findViewById(R.id.StoreCatItem);
// TitleView
View TitleView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(TitleView);
Button titleButton = (Button)
TitleView.findViewById(R.id.StoreCatItem);
// ProfileView
View ProfileView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(ProfileView);
Button profileButton = (Button)
ProfileView.findViewById(R.id.StoreCatItem);
I simply Created Multiple Views Programmatically, and then retrieved the buttons from those views afterwards. I then added the listeners to the buttons as follows:
perkButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getBaseContext(), Perks.class);
i.putExtra("player", player);
startActivity(i);
}
});
titleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getBaseContext(), Titles.class);
i.putExtra("player", player);
startActivity(i);
}
});
Thanks A lot :)
There are two ways you can handle the button click distinctly,
At the time of inflating layout for each store, add the button listener with onClick() method there only.
Or you can assign some uniqueId or flag to each store and then handle button click of store by this uniqueID.
Usually you solve this by following stack:
fragment -has-> list -has-> adapter -has-> list of items
Normally if you present one item per screen and you want to swipe between you should use ViewPager with FragmentStatePagerAdapter (allows removing items from ViewPager if needed).
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
For more items in one screen use ListView with BaseAdapter as it allows better control over item view and ListView will recycle views as you scroll or fling.
http://developer.android.com/guide/topics/ui/layout/listview.html
Set unique tag for each store button, in code you can differentiate with respect to tags.Tag is just piece of information you want set for any view, u can use it.Implement on click listener in your activity and then set that to all buttons, so all buttons click run through same code where you can easily diffrentiate between ur store buttons with respect to tags.
like below how you can set and get tag
// for setting tag
Button button = (Button) findViewById(R.id.button1);
button.setTag("unique_tag");
// get tag and then differentiate with the unique_tg
button.getTag();
You can easily create your button programmatically like so :
Button b = new Button(this); // where this = your context
b.setText("Buy");
// b."other attribute" = "other value";
b.setTag("Awesome blue shirt");
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Button buy = (Button) v;
String article = b.getTag();
// do some stuff
}
});
container.addView(b);
You can identify the buy button using its tag, as seen here.

Android: How to propagate click event to LinearLayout childs and change their drawable

I would like to create a linear layout which would behave similarly to ImageButton.
<LinearLayout
android:id="#+id/container"
style="?WidgetHomeIconContainer">
<ImageView
android:id="#+id/icon"
style="?WidgetHomeIcon" />
<TextView
android:id="#+id/title"
style="?WidgetHomeLabel"
android:text="#string/title"
android:textAppearance="?attr/TextHomeLabel" />
</LinearLayout>
In styles of ImageView, TextView and LinearLayout, I set a selectors for all states.
Now:
when I click on ImageView (I tried it also with ImageButton) - it behaves correctly and the image is changed according the selector xml.
when I click on LinearLayout - the linear layout is clicked, but the the ImageView and TextView don't change it's drawable/appearance
So I would like to do the following. When I click on parent LinearLayout, I need to change all it's childs to pressed state.
I tried to add following code to LinearLayout onClickListener to propagate the click:
#Override
public void onClick(View v)
{
LinearLayout l = (LinearLayout) v;
for(int i = 0; i < l.getChildCount(); i++)
{
l.getChildAt(i).setClickable(true);
l.getChildAt(i).performClick();
}
}
But it still reamins the same. Thank you very much for any help.
Put
android:duplicateParentState="true"
in your ImageView and TextView..then the views get its drawable state (focused, pressed, etc.) from its direct parent rather than from itself.
Not only make for every child:
android:duplicateParentState="true"
But also additionally:
android:clickable="false"
This will prevent unexpected behaviour (or solution simply not working) if clickable child views are used.
SO Source
After having the same problem some months later, I found this solution:
private void setOnClickListeners() {
super.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClick(v);
}
});
for (int index = 0; index < super.getChildCount(); index++) {
View view = super.getChildAt(index);
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClick(v);
}
});
}
}
protected void onClick(View v) {
// something to do here...
}
In my case, no one of the other solutions works!
I finally had to use OnTouchListener as explained here, capturing the event when the user clicks in the parent view, and removing all childs OnClickListener.
So the idea is, delegate the click behavior to the parent, and notify the child that is really clicked, if you want to propagate the event. ¡¡That's what we are looking for!!
Then, we need to check which child has been clicked. You can find a reference here to know how it´s done. But the idea is basiclly getting the area of the child, and asking for who contains the clicked coordinates, to perform his action (or not).
At first, my child view failed to get click from parent. After investigating, what I need to do to make it work are:
remove click listener on child view
adding click listener on parent view
So, I don't need to add these on every children.
android:duplicateParentState="true"
android:clickable="false"
I only add duplicateParentState to one of my child view.
My child view is now listening to parent click event.

Android, screwy behavior

Cant figure this out (am a noob) but I have a default activity that starts with just one image button, clicking that image button opens the second activity that contains a bunch of image buttons but only one has an onclick, this is the exact code:
<ImageButton android:src="#drawable/level1"
android:layout_width="wrap_content" android:id="#+id/imageButton1"
android:layout_height="wrap_content"
android:onClick="button_clicked1">
</ImageButton>
in the java file this is my code:
public void button_clicked1(View v) {
//text1.setText("clicked");
//ib2.setImageResource(R.drawable.level5);
}
The screwy part, if that above function is empty nothing happens but no matter what I put in the above function... for example even a simple settext like the above commented one, and it force closes on me :((
the full java file if you are interested: http://pastebin.com/W4sJUKXH
and the manifest file (as the problem might be there, like I said, am a noobie) http://pastebin.com/yEuG1su7
** Where i think the error is:
In the manifest file I only have declared the second activity, nothing else...
Set id for the ImageViews, this is how you handle many Buttons with one method
public void button_clicked1(View v) {
switch(v.getId())
{
case R.id.myfirstimage:
//do something
break;
case R.id.mysecondimage:
// do something
break;
// add more cases
default:
// do something if none of the cases is your image view or do nothing
}
}
Set setContentView( ) before you do anything with the UI components. (Thats why you get some Exception)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_levels_screen);
text1 = (TextView) findViewById(R.id.textView1);
ib2 = (ImageButton) findViewById(R.id.imageButton2);
}

Categories

Resources