Showing a List View when a button is clicked: Android - android

I am trying to implement a drop down list when a button is clicked.
So, I have a text view and a button in a navigation bar(nav.xml) and a corresponding list view. This navigation bar is included in another page( products.xml)
when the button is clicked i get the list view right below the button(which is what i want to acheive) but its my moving all the contents on the current page downwards, even the text view which is placed in nav bar moved downwards.
I am totally new to Android, any sample examples or a way how to achieve it
???

Sounds like you need a Spinner. It's the equivalent of a drop down list for Android. You can find an example here.

So, for our need we need to use ListPopupWindow.
The link to official description:
http://developer.android.com/reference/android/widget/ListPopupWindow.html
Let's dive in the code:
we have our own method:
public void downloadBtnSelected(View anchor) {
final ListPopupWindow lpw = new ListPopupWindow(this);
String[] data = { ".png", ".pdf", ".jpg", ".jpeg" };
PopupAdapter pa = new PopupAdapter(data, this);
lpw.setAdapter(pa);
//setting up an anchor view
lpw.setAnchorView(anchor);
//Setting measure specifications. I'v used this mesure specs to display my
//ListView as wide as my anchor view is
lpw.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
lpw.setWidth(anchor.getRight() - anchor.getLeft());
// Background is needed. You can use your own drawable or make a 9patch.
// I'v used a custom btn drawable. looks nice.
lpw.setBackgroundDrawable(this.getResources().getDrawable(
android.R.drawable.btn_default));
// Offset between anchor view and popupWindow
lpw.setVerticalOffset(3);
lpw.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
/// Our action.....
lpw.dismiss();
}
});
lpw.show();
}
and the button with an onClickListener to call this method:
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
downloadBtnSelected(v);
}
});
we pass the View v argument as our anchor, in order to let our PopupWindow to know where to display itself. It will be displayed in the bottom-left corner of our anchor view if there is enough room below. If not- it will displa

Related

Popup window on a ListView Android

I have created a custom arrayadapter for my listview. It has a player name and the score, I also have a add button on the cell, when that button is clicked I want a popup screen to appear, Here is where the user can add the score of the player. This popup window has 6 buttons "+1", "+2" "+10" ..etc and a done button. When the done button is clicked the score gets updated.
I am handling the add button click event on my customArraryAdapter class, so I have to create the popup here as well. I have searched and tried to do this with no success.
What I tried so far:
I have a View = convertView and a viewHolder = holder The problem is I'm not so sure what to pass as the parameter to create a popup. The code below is myCustomArrayAdapter class. I have also read popups cant handle touch events, but some are saying it can. Since my popup has a lot of buttons maybe this might be a great solution.
This is in #Override
public View getView(final int position, View convertView, ViewGroup parent) Method in CustomArrayAdapter Class
//Handle add button click
holder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
addScores(convertView);
//list gets updated
notifyDataSetChanged();
}
});
My addScores method looks like this
private void addScores(View v){
PopupWindow pw;
LayoutInflater inflater = (LayoutInflater)v.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.weight_popup, (ViewGroup)v.findViewById(R.id.linlay_weight_popup));
pw = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setOutsideTouchable(true);
pw.showAsDropDown(btnSelectWeight);
}
You may pass View that will be displayed in popup the same way you do.
Consider this:
View layout = inflater.inflate(R.layout.weight_popup, (ViewGroup)v.findViewById(R.id.linlay_weight_popup));
Your weight_popup layout should contain 6 buttons, which would have onClick there you update scores.
Something like:
Button btn1 = (Button)layout.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//update your score here.
}
});
//other buttons..
pw = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

Android items background automatically change

today i was implementing a new feature in my app: showing a dialog with an EditText and requesting the soft keyboard, and i notice something really strange with my items in the sliding menu (I'm using the library from jfeinstein10)...
When the dialog shows and not the keyboard the items inside the sliding menu are okay, but when i also show the keyboard, the items change their background resource...
I tried using the log in the item click listener of the list inside the sliding menu but nothing it's called... So... Why it automatically change the background ?!
Hope you can help me :) And sorry for my english :D
The item click listener inside the sliding menu:
private AdapterView.OnItemClickListener slidingListItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Unselected items
for (int j = 0; j < adapterView.getChildCount(); j++) {
View root = adapterView.getChildAt(j);
ImageView image = (ImageView) root.findViewById(R.id.partial_sliding_menu_list_image);
TextView text = (TextView) root.findViewById(R.id.partial_sliding_menu_list_text);
image.setBackgroundResource(SLIDING_MENU_ICON_NORMAL);
text.setTextColor( getResources().getColor(SLIDING_MENU_TEXT_NORMAL) );
}
// change the drawable of the selected item
ImageView image = (ImageView) view.findViewById(R.id.partial_sliding_menu_list_image);
TextView text = (TextView) view.findViewById(R.id.partial_sliding_menu_list_text);
image.setBackgroundResource(SLIDING_MENU_ICON_SELECTED);
text.setTextColor( getResources().getColor(SLIDING_MENU_TEXT_SELECTED) );
// Load the page clicked [it's a fragment.]
loadPage(i);
}
};
Some screenshots of the "error":
Click me

I have 200 textviews and i want to know which one was pressed then how to change the text

I have a problem... have been thinking about it for a while now and been looking on line and still haven't come up with a clear explanation...
I have a number of textviews and have set onClickListeners to each of them.. and when the user clicks on one of them I want them to have the ability to change the text to another set of string array options which I have created progammatically. When the user selects an option the text should change to the option they choose. (I.e. TextView was A now it is B. hope this makes sense.. anyway... )
The current solution was to set a OnClickListener to every TextView and when someone pressed it an individual dialog showed. But I found that if I do this the code would be so long it would take an eternity to code so am hoping someone has a more elegant way of coding such a long process =(
So I guess my question would be... 1) is there a way I can find out which text view was pressed and then change the text of that TextView being pressed within a single method? to save me having to code 1000 alert dialogs...
http://i.stack.imgur.com/LRJGz.png
I would advise you to use a grid view.
You can see which textview was pressed like this:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
//get id
switch (v.getId()) {
case R.id.textView1: ...
}
});
One of the ways to do what you want is to use the text view setTag() and getTag() methods.
On init of a text view use the setTag() to set some value to identify the view.
In the on click event use the getTag() on the view argument to know which view was clicked.
I would suggest holding the textviews in an array, like so:
TextView[] textViewArray = new TextView[textViewCount];
Then using a for loop assign each one a tag of integer - it's position
textViewArray.setTag(i)
And add an onClickListener to each one, again using a for loop:
textviewArray[i].setOnClickListener(etc...)
Then when one is clicked, you can use get the position of view that was clicked. This will require a custom method inside of your:
textviewArray.setOnClickListener(new customOnClickListener())
Where your customOnClickListner is like this:
private class customOnClickListener implements CompoundButton.{
public void OnClick(View view){
int position = (Integer) view.getTag()
///Do more code here - your processing
}
}
Hope that makes sense :)
For your for loops, you could use for(i = 0, i
Use set id for all text, where set the id positive integer(distinct), and then have one on view click listener(set it all) where u catch all text view clicks(downcast view with textview) and in side it put a switch case where you handle clicks on which text view is clicked.
You have to set "onClickListner" on all of of your textview.
For Saving some length of code i would suggest you create a function of your dialogbox, and give some int parameter to it, which would be directly called by the clickListener of textview,
Like ,
int i=0;
......
textView1 = (TextView)findViewById(R.id.yourtextview1);
textView2 = (TextView)findViewById(R.id.yourtextview2);
......
......
// and so on, for your all textviews
#Override
public void onClick(View view) {
if (view.equals(textView1)) {
i = 1;
CustomDialog(i);
}
//Similarly for all your textViews..
..........
Make A function CustomDialog Like
public void CustomDialog(int i){
if(i==1){
//Do something
}
}

setting text in Gallery onItemSelected causes scrolling to snap

In the galleryView onItemSelected I call setText that change the text for a textview thats part of the main layout:
#Override
public void onItemSelected(EcoGalleryAdapterView<?> parent, View view, int position, long id) {
// --- run asyncTask to update gallery view here
TextView myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setText("position is: ": position);
}
if I left everything as it is and just removed myTextView.setText the gallery works as expected but if I kept it then when scrolling the gallery snaps to the selected position really fast in an ugly way. What could be the issue?
"Ugly" is a pretty subjective term for describing a user interface transition.
However, it sounds as though what you want is a custom animation when an item is selected. onItemSelected() is called before the layout happens, so you can animate your Gallery or individual Views however you want in this method.
I would suggest reading the Animation and Graphics documentation from the Android developer documentation to more fully understand animations and to help decide what you actually want.
The code will vary depending on what you decide you want it to look like and what version of Android you are targeting. A simple View animation that will fade the selected view in might look something like this:
public void onItemSelected(EcoGalleryAdapterView<?> parent, View view,
int position, long id) {
view.setAnimation(new AlphaAnimation(0, 1));
}
// try this
TextView myTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_full);
myTextView = (TextView) findViewById(R.id.myTextView);
}
#Override
public void onItemSelected(EcoGalleryAdapterView<?> parent, View view, int position, long id) {
// --- run asyncTask to update gallery view here
myTextView.setText("position is :"+ position);
myTextView.invalidate();
}
The setText commands sits on the UI Thread, maybe it's taking a higher priority or something from the current Gallery animation which disrupts it from acting as it should.
try setting your setText inside a handler:
new Handler().post(new Runnable() {
#Override
public void run() {
myTextView.setText("position is: ": position);
}
});

GridView : Get a view by position

I'm using a grid view to display a month calendar. When i touch a date on the calendar , the corresponding cell is highlighted by changing its background and text Color. Everything works like charm except the fact that i can't un-highlight a cell if i choose a new date.
When i click a date i remember the position, so when i chose a new one i know where i clicked previously :
protected OnClickListener onClicThisMonthDate(final int position)
{
return new OnClickListener() {
#Override
public void onClick(View v)
{
if(prevDateSelected > -1 )
{
//Get the view by the oldPosition
//Change its background and text color
}
prevDateSelected = position;
TextView casecal = (TextView) v.findViewById(R.id.case_cal);
casecal.setBackgroundColor(Color.rgb(93, 94, 94));
casecal.setTextColor(Color.WHITE);
}
};
}
Is there a solution to get a view by his position ? What's the most efficient way to select a cell and unselect all the other ?
Note : By "select a cell" i mean highlight it, by changing it style.
ViewGroup.getChildAt returns the view at the specified position in the group.
GridView.setSelection selects of the view at the position and unselects others views.

Categories

Resources