I have a ListView with custom list items. Each list item consists of 2 linear layouts one next to other.
LinearLayout 1 | LinearLayout 2 |
I've declared state list drawables for both LinearLayouts where in state_pressed I'm changing the background of the LinearLayout.
And here comes the issue - When the user taps on the LinearLayout2 only the background of LinearLayout2 should be changed, the background of LinearLayout1 should remain unchanged. On the other hand, when the user taps on LinearLayout1, only the background of LinearLayout1 should be changed. But now when the user taps on either of both LinearLayouts, both of them change their background.
The behaviour on tap on LinearLayout2 should be as onListItemClick() while when the user taps on LinearLayout1 a Dialog should appear (if this matters).
Any ideas how could I solve the background change issue? I've tried playing with focusable and clickable options. If i set clickable=true to both LinearLayouts, the children (TextViews) of LinearLayout2 do not change their colour (the TextViews should change their text colour).
Thank you!
This is because when using a list view you have to change some tags in XML to make the background transparent so that it will correctly work with your back ground.
Add this to your ListView XML code.
android:cacheColorHint="#00000000"
To set the ListView's background to transparent.
Well I think a single solution if you are using BaseAdapter as extends
First give unique Id to both those Layouts in you xml file and add
android:clickable="true"
In your method
public View getView(int position, View convertView, ViewGroup parent) {
when your are getting those views like
holder.layout1_name=(LinearLayout)view.findViewById(R.id.layout1);
holder.layout1_name.setOnClickListener( clicklayout1);
holder.layout2_name=(LinearLayout)view.findViewById(R.id.layout2);
holder.layout2_name.setOnClickListener( clicklayout2);
Add click listener on them
private OnClickListener clicklayout1 = new OnClickListener() {
public void onClick(View v) {
//Do what you want to do here
}
};
private OnClickListener clicklayout2 = new OnClickListener() {
public void onClick(View v) {
//Do what you want to do here
}
};
May be this may help you
Related
Hello my name is Fabian and at the moment I try to programming an Android App.
I have a ListView with some items and above this ListView I added a LinearLayout, which I want to make clickable, to add items to the ListView.
If I touch one entry in the ListView the item gets colorized with the default color, a light grey. I like to have this behaviour for my LinearLayout, too.
I know how to get the LinearLayout clickable.
I did this by
android:clickable="true"
android:onClick="addProject"
Also I know how to define the backgroundcolor, but I don't know how I can pass the default colors from android of the ListView (android:listSelector) to the LinearLayout.
I tested to define
android:background="?android:attr/listChoiceBackgroundIndicator"
but in that case the LinearLayout gets colorized blue and only if I touch it, it gets the right color.
I hope you can help me, to pass the default color of a ListView to a LinearLayout.
Remove below line from your LinearLayout
android:onClick="addProject"
and add an id to a LinearLayout,
android:id="#+id/linear_layout"
From JAVA add this,
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);
layout.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Toast.makeText(this, "Layout Clicked", Toast.LENGTH_LONG).show();
}
});
I have a layout (activity_main.xml) that has some TextView and EditText elements and a button.
I have some stuff happen when the button is clicked, but I want to add a new EditText element directly below and existing element after the button has been clicked.
How can I make this happen please?
Usually i just play around with the Visibility of the elements. Example:
on OnCreate: EditText1.setVisibility(GONE);
and then on your OnClickListener Implementation:
private OnClickListener onShow = new OnClickListener() {
public void onClick(View v) {
EditText1.setVisibility(VISIBLE);
}
}
This will hide your EditText (or whatever element you want) in the creation of the activity, and then show them again when you pressed the button.
The above mentioned method is easier and it seems suffice enough for most of my projects. However, if we really want to add elements dynamically, there is a way.
We can basically add any element dynamically to our xml layout. But we need an element (container) in our xml layout for holding our added element later. Example, we use an empty LinearLayout with android:id="#+id/container". With this in mind, it means we can build everything dynamically from scratch and setContentView(ourView), where ourView is the root element with other child elements added.
Example:
EditText newElement = new EditText(this);
newElement.setLayoutParams(params);
LinearLayout container = (LinearLayout)findViewById(R.id.container);
container.addView(newElement);
Note: This is just a pseudocode and not a complete code.
I am actually developing an android application which needs to inflates many times the same XML layout.
This layout contains 2 buttons, some textViews and a progressBar which I'll need to update later. I would like to add onClick listeners to the buttons and to set custom tags with setTag() to all of these elements, so I will be able to know which button has been clicked and to modify the right textView (or progressBar).
I inflate the XML with this code :
LinearLayout countersList = (LinearLayout)findViewById(R.id.countersLayout);
View child = getLayoutInflater().inflate(R.layout.counter, null);
countersList.addView(child);
How can I access to the right view to set the tag and to add listeners? Is there a better way to do what I want to do ?
Thank you very much !
As far as how to tag or add onClick listeners to your views: You can add an ID to the views you want to tag and find them using findViewById. For example,
LinearLayout countersList = (LinearLayout)findViewById(R.id.countersLayout);
ViewGroup child = (ViewGroup) getLayoutInflater().inflate(R.layout.counter, null);
child.findViewById(R.id.myButton).setTag("myTagForTheButton");
countersList.addView(child);
On the second question, I'm not sure what your UI looks like, but many repeated views might call for using a ListView.
There is no problem of setting tags and listener
LinearLayout countersList = (LinearLayout)findViewById(R.id.countersLayout);
View child = getLayoutInflater().inflate(R.layout.counter, null);
child.setTag("YourString");
// Similarly
view.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
countersList.addView(child);
If you want it for some of its child you can also do it using findViewById
you can user view.setTag(key, tag) to many tags.
ex:
view.setTag("right_view", rightView);
I have a RelativeLayout positioned over the rest of my view and I want this RelativeLayout to stop anything underneath it from being clicked. Here is my document outline:
FrameLayout
LinearLayout
RelativeLayout
The RelativeLayout only has a background colour, and has nothing inside it. The LinearLayout has buttons and textboxes in it. I want to stop people from being able to click on the buttons and textboxes if the RelativeLayout is visible.
You will need to consume the click. Find yourRelativeLayout and set an OnClickListener on it:
yourRelativeLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { }
});
In the xml file
android:clickable="false"
To do it in code:
LinearLayoutItem.setClickable(false);
I am displaying a ListView using a custom adapter. In the view i have a textView and a button in a layout.
What is supposed to happen -> As soon as i click on the text view the onClick callback in the custom adapter class for the TextView sets the margin for the button_layout like so
View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout));
MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams();
margins.bottomMargin=-100;
But this is not happening.I am able to change the background color. But not able to change the bottom margin. The customadapter is a part of the larger code which i cannot disclose.
The app doesnt crash but doesnt work either. If i see in the debugger the value of bottomMargin for the layout has changed but it is not reflected in the UI :( I have put some part of the code here. assume that the onClickListener has been set. It is working because as i said i am able to change the background color of the layout on Clicking the Text View.
public class MyCustomAdapter extends ArrayAdapter<someClass> implements OnClickListener{
public View getView(int position, View convertView, ViewGroup parent) {
}
public void onClick(View v){
View row_to_hide = (((View ((View)v.getParent()).getParent()).findViewById(R.id.row_to_hide));`
MarginLayoutParams margins=(MarginLayoutParams)row_to_hide.getLayoutParams();`
margins.bottomMargin=-100;`
}
}
I am new to android and would like to know if there something conceptually wrong with this approach. Also the textView and the button are in a Relative layout and a Linear layout respectively.
I am trying to change the margins of the linear layout in which the button is situated so that i can hide the button.
You changed layout params but you have to set them back to view. So method setLayoutParams is missing there. Code shold look like this:
View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout));
MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams();
margins.bottomMargin=-100;
button_layout.setLayoutParams(margins);
Side note: You should replace ((View)(View)v.getParent()).getParent()) with convertView parameter from getView method.