In my application i am displaying rows dynamically.
I am using the below code so as to focus the clicked row even if the user press back key.
But the issue is like if the row is not focussed we need two clicka to navigate.First to make it focussable and then next click to navigate.I need all at once any suggestions.
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int a=0;a>16;a++)
{
tr[a].setBackgroundColor(Color.BLACK);
}
flag=v.getId();
if(v.getId()==1)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==3)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==5)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==7)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==100)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
for(int a=0;a>16;a++)
{
tr[a].setBackgroundColor(Color.BLACK);
}
if(hasFocus)
{
((TableRow)v).setBackgroundColor(Color.rgb(255, 180, 40));
}
else
{((TableRow)v).setBackgroundColor(Color.BLACK);}
}
protected void onResume() {
super.onResume();
for(int a=0;a>16;a++)
{
tr[a].setBackgroundColor(Color.BLACK);
}
tr[flag].requestFocus();
tr[flag].setFocusableInTouchMode(true);
tr[flag].setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus)
{
((TableRow)v).setBackgroundColor(Color.rgb(255, 180, 40));
}
else
{((TableRow)v).setBackgroundColor(Color.BLACK);}
}
});
}
#Override
public void onPause() {
super.onPause();
}
Please let me know your valuable suggestions.
Thanks in advance.
If your only need for focus listener is to change background colors, you would do better setting their background to a drawable that has different backgrounds for focused and normal states, then you don't need to control it in code at all. As for requiring two clicks to navigate, I don't see anything in your code about it. It seems to start an activity as soon as a view is clicked once.
Related
I saw many posts on this subject but couldnt find the right answer for me.
I have an activity that dims when i pop the popup window.
the back button working but only the second time i press it, the first press dismiss the popup but its not un-dim the activity because i cant catch the event from the popupwindows, the second press is catched by the activity and only then i can un-dim it.
here are my tries to accomplish this:
m_PopupWindow.setBackgroundDrawable(new BitmapDrawable());
m_PopupWindow.setOutsideTouchable(true);
View popUpWindowLaout = m_PopupWindow.getContentView();
popUpWindowLaout.setFocusableInTouchMode(true);
//first press doesnt get caught here
popUpWindowLaout.setOnKeyListener(new View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
m_ActionBar.show();
unShadeTheActivity();
m_PopupWindow.dismiss();
return true;
}
}
});
//this func will catch the second press and will work, but i want the first press will do it.
#Override
public void onBackPressed() {
if (m_PopupWindow != null)
{
m_ActionBar.show();
unShadeTheActivity();
m_PopupWindow.dismiss();
}
else
{
super.onBackPressed();
}
}
change
public void onBackPressed() {
if (m_PopupWindow != null)
{
m_ActionBar.show();
unShadeTheActivity();
m_PopupWindow.dismiss();
}
else
{
super.onBackPressed();
}
}
to
public void onBackPressed() {
super.onBackPressed();
if (m_PopupWindow != null)
{
m_ActionBar.show();
unShadeTheActivity();
m_PopupWindow.dismiss();
}
else
{
// rest of the code
// you can use finish,dismiss or call startActivity
// finish();
}
}
popupWindow.setOnShowListener(HandlePopupShowLister);
popupWindow.setOnDismissListener(HandlePopUpDismissListerner);
public static OnDismissListener HandlePopUpDismissListerner = new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Log.i("HandlePopUpDismissListerner", "HandlePopUpDismissListerner");
CommonVariable.IsPopupOpen = false;
}
};
public static OnShowListener HandlePopupShowLister = new OnShowListener() {
// onShowListener interface.
#Override
public void onShow(DialogInterface dialog) {
Log.i("HandlePopupShowLister", "HandlePopupShowLister");
// TODO Auto-generated method stub
CommonVariable.IsPopupOpen = true;
}
};
I am having problem on back button of hardware. In my main Activity, I have one List view(say 1). When I click on item of this List view(1), one Alert Dialog appears, in this alert dialog, there is one List view(say 2). Data of this List view(2) are being repeated when I press back button of hardware. I have also put cancel image on this alert dialog to dismiss, when I press this cancel image, data are not being repeated. I tried different methods onResume(), onPause(), onDestroy(), onRestart() to clear the array for List view(2), but nothing works. Here is my code...
case LIST_DIALOG :
LayoutInflater inflater2 = LayoutInflater.from(this);
View dialogview1 = inflater2.inflate(R.layout.listdialog, null);
AlertDialog.Builder dialogbuilder2 = new AlertDialog.Builder(this);
dialogbuilder2.setView(dialogview1);
dialogDetails = dialogbuilder2.create();
case LIST_DIALOG:
AlertDialog alertDialog1 = (AlertDialog) dialog;
// Cancel Alert Dialog
ImageView ivCancel = (ImageView) alertDialog1.findViewById(R.id.imgCancel);
ivCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dismissDialog(LIST_DIALOG);
arr2.clear();
}
});
// Friend List
showFriendList();
break;
// List view data are inserted with this function call
private void showFriendList() {
// TODO Auto-generated method stub
Request.executeMyFriendsRequestAsync(friendSession, new GraphUserListCallback() {
#Override
public void onCompleted(List<GraphUser> users, Response response) {
// TODO Auto-generated method stub
// arr2 = new ArrayList<String>();
for(GraphUser user : users)
{
arr2.add(user.getName());
}
adapter2 = new ArrayAdapter<String>(getBaseContext(), R.layout.single_row, R.id.txt,arr2);
lvDialog.setAdapter(adapter2);
lvDialog.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapter2.notifyDataSetChanged();
itemCount = lvDialog.getCount();
Toast.makeText(getBaseContext(), "" + itemCount, 1000).show();
}
});
}
// I tried these methods, but nothing works...
#Override
public void onResume()
{
super.onResume();
ShowSavedFiles();
arr2.clear();
}
#Override
public void onPause()
{
super.onPause();
arr1.clear();
arr2.clear();
}
#Override
public void onBackPressed() {
//super.onBackPressed();
// finish your Activity
arr2.clear();
return;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
arr2.clear();
dismissDialog(LIST_DIALOG);
}
return false;
}
I'm not sure but might be adding the following code to your onKeyDown method might help out :
return super.onKeyDown(keyCode, event);
i am using java android .
i have a problem in my app and i need some code that can make my item has two lines of texts and one image .
please save me and help .
here is the code for what i tried please make sure that you can help and give your opinion and how i can solve it cause i stucked here.
the main activity
private void setUpView() {
// TODO Auto-generated method stub
etInput = (EditText)this.findViewById(R.id.editText_input);
Pinput = (EditText)this.findViewById(R.id.editText1);
btnAdd = (Button)this.findViewById(R.id.button_add);
lvItem = (ListView)this.findViewById(R.id.listView_items);
itemArrey = new ArrayList<String>();
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,itemArrey);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
etInput.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER){
addItemList();
}
return false ;
}
});
Pinput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(keyCode== KeyEvent.KEYCODE_NUM) {
addItemList();
}
return;
}
});
}
protected void addItemList() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (isInputValid(Pinput )) {
itemArrey.add(Pinput.getText().toString());
itemArrey.add(etInput.getText().toString());
Pinput.setText("");
etInput.setText("");
itemAdapter.notifyDataSetChanged();
} if (isInputValid(etInput )) {
itemArrey.add(Pinput.getText().toString());
itemArrey.add(etInput.getText().toString());
Pinput.setText("");
etInput.setText("");
itemAdapter.notifyDataSetChanged();
}
}
protected boolean isInputValid(EditText etInput2 ) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
return false;
} else {
return true;
}
}
}
If you want a custom view in your ListView I recommend doing one xml layout by yourself and replace android.R.layout.simple_list_item_1 in your Adapter call with the new created one.
This would be the easiest way but as I think that you might need more freedom to create/modify the content of the row, I strongly suggest to create your own Adapter extending from BaseAdapter. In the getView() method of the adapter you can easily inflate and set up the layout like you want for that row.
Okie... you tag android-listview and list view item. So I think you need list view with image and and Text.
For that you have to use Custom List View.
See this
May be this will help you. and not then pls make your que more clear. :)
I am trying to get values from server and retrieved response from server, the details responsed from server are need to be displayed in a listview, i achieved that too, but while onitemclick process only first row in list view performs the click action but it is not performing for the remaining rows, how can i achieve it, dat is whatever the row and item i'm selecting it has to be processed by my code, here is my code for the reference, .........
public void userInterface() throws JSONException
{
if(json_user.get(0) != null)
{
downloadList1 = (ListView)findViewById(R.id.lvinbox);
populateData();
downloadList1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub
fma=(TextView)findViewById(R.id.src);
st=(TextView)findViewById(R.id.sub);
fma.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
String fm=fma.getText().toString();
String s=st.getText().toString();
Intent m = new Intent(getApplicationContext(), InActivity.class);
m.putExtra("f", fm);
m.putExtra("s", s);
startActivity(m);
}
});
st.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
String fm=fma.getText().toString();
String s=st.getText().toString();
Intent m = new Intent(getApplicationContext(), InActivity.class);
m.putExtra("f", fm);
m.putExtra("s", s);
startActivity(m);
}
});
}
});
}
else
{
Toast.makeText(getApplicationContext(),"Empty", Toast.LENGTH_LONG).show();
finish();
Intent menu = new Intent(getApplicationContext(), MenActivity.class);
menu.putExtra("username", dest);
startActivity(menu);
}
}
public void populateData() throws JSONException
{
resultVector = new Vector<ListDataItemInbox>();
int len = json_user.length();
System.out.println("length--------------------->"+len);
for(int i=0;i<len;i++)
{
JSONObject obj=json_user.getJSONObject(i);
listData = new ListDataItemInbox();
listData.sets(obj.getString(KEY_S));
listData.setd(obj.getString(KEY_D));
resultVector.add(listData);
myHandler.post(myRunnable);
}
}
private Handler myHandler = new Handler();
private Runnable myRunnable = new Runnable()
{
public void run()
{
customListAdapter = new ListViewAdapterInbox(InActivity.this);
customListAdapter.setResultsData(resultVector);
downloadList1.setAdapter(customListAdapter);
}
};
You can do something like this.
make the ArrayList in list adapter class
ArrayList<Integer> selectedIds = new ArrayList<Integer>();
and create this method also in that same class.
public void toggleSelected(Integer position){
selectedIds.clear();
selectedIds.add(position);
}
Now whenever you click on any item of listview pass that item position to this method. And inside your getView method set the check before setting the text. something like this..
if (selectedIds.contains(position)){
----- your code ----
}else{
----- your code ----
}
it perform the operation only on selected position . Try it.!
I am new to android development.
I have on one image in layout.I used scale animation for that image.
and i am able to stop the scaling of image at particular point. now i want to resize that image on another clicklistener.
How to do that? if any idea,help.
Here is my code.
final ImageView img_graph= (ImageView)findViewById(R.id.graph01);
final Animation AnimationScale= AnimationUtils.loadAnimation(this,R.anim.anim_scale);
final Animation AnimationScale_reverse= AnimationUtils.loadAnimation(this,R.anim.anim_scale_reverse);
if(flag ==FLAG_SCALE_IN) {
if(resp==0){
img_graph.setOnClickListener(new ImageView.OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
img_graph.setBackgroundResource(R.drawable.page01_graph);
img_graph.startAnimation(AnimationScale);
}});
}
}
if(flag==FLAG_SCALE_OUT) {
if(resp==1){
img_graph.setOnClickListener(new ImageView.OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
img_graph.setBackgroundResource(R.drawable.page01_graph);
img_graph.startAnimation(AnimationScale_reverse);
}});
}
}
can't we handle this case using if else in same listner ?
img_graph.setOnClickListener(new ImageView.OnClickListener(){
public void onClick(View arg0) {
if(resp==0 && flag ==FLAG_SCALE_IN) {
img_graph.setBackgroundResource(R.drawable.page01_graph);
img_graph.startAnimation(AnimationScale);
}else if( resp==1 &&flag ==FLAG_SCALE_OUT) {
img_graph.setBackgroundResource(R.drawable.page01_graph);
img_graph.startAnimation(AnimationScale_reverse);
}
}
}});
More optimization:
public void imageViewClick(View v)
{
if(flag==FLAG_SCALE_IN && resp==0)
{
}
else if(flag==FLAG_SCALE_OUT && resp==1)
{
}
}