I have a imageView in onCreate method where I am using method showList. This is code:
action_bar_log_in_icon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
allResults.clear();
allResults.add(action_bar_log_in_icon);
allResults.add(action_bar_data_privacy_icon);
allResults.add(action_bar_vCard_icon);
allResults.add(action_bar_email_icon);
showList();
}
});
and code method showList:
private void showList() {
final ListDialog listDialog = new ListDialog(this);
listDialog.setCanceledOnTouchOutside(true);
final ListView lv = (ListView) listDialog
.findViewById(R.id.spinnerlist);
if(lAdapter == null){
lAdapter = new ListAdapter(getBaseContext(),
R.layout.spinner_item,
(ArrayList<ImageView>) allResults);
}
lv.setAdapter(lAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch(position){
case 0:
intent = new Intent(ActionBarMain2.this, LoginActivity.class);
startActivity(intent);
break;
case 1:
intent = new Intent(ActionBarMain2.this, LoginActivity.class);
startActivity(intent);
break;
case 2:
intent = new Intent(ActionBarMain2.this, vCard_view.class);
startActivity(intent);
break;
case 3:
intent = new Intent(ActionBarMain2.this, EmailView.class);
startActivity(intent);
break;
}
listDialog.dismiss();
}
});
WindowManager.LayoutParams WMLP = listDialog.getWindow()
.getAttributes();
WMLP.gravity = Gravity.TOP | Gravity.RIGHT;
WMLP.y = 50;
WMLP.width = 100;
listDialog.getWindow().setAttributes(WMLP);
listDialog.show();
}
This solution work and if I click on imageview my list is showing, but problem is when I click again on the same imageview. List is still visible. How I can do when I click first time my list is showing and when I click again my list dismiss etc?
What if you create your ListDialog just once in your Activity's onCreate not showing it yet.Then upon clicking your ImageView you can do if(dialog.isVisible()) { dialog.show(); } else { dialog.dismiss(); }
set some flag for capturing the state of the dialog. Then set condition on that flag..
If its visible then set dismissDialog(dialogName);
I find solution my problem. This dialog cover my imageView, so I placed dialog a little lower. Thank you for advices.
Related
I'm new to android and I'm having a hard time finding solutions to my problem on my app.
My app is a wordsearch game that uses tapping on the tiles as an input. This is the code for the onClick() of the dynamic textviews on the tablelayout:
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
w.setVisibility(View.VISIBLE);
//change the color of tapped textview
text.setTextColor(Color.WHITE);
text.setBackgroundColor(Color.parseColor(mColors[randomNum]));
String b = text.getText().toString();
uTxt.setText(""+uTxt.getText().toString() + b);
//check if answer is in the word grid
if(checkAns(uTxt, list))
{
w.setVisibility(View.GONE);
wC.setText(String.valueOf(Integer.parseInt(wC.getText()+"")-1));
if(Integer.parseInt(wC.getText()+"") == 0){
int newM = minutes*60 + seconds;
dataHelper.insertData(pNameC.getText().toString(), newM, currentDateandTime, Category.leve);
t.cancel();
Context mContext = getApplicationContext();
Activity mActivity = GameScreen.this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflate the custom layout/view
View customView = inflater.inflate(R.layout.gameover,null);
// Initialize a new instance of popup window
PopupWindow mPopupWindow = new PopupWindow(
customView,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
Typeface font = Typeface.createFromAsset(getAssets(), "raw2.ttf");
TextView cattxt = (TextView)customView.findViewById(R.id.catTxt);
String ctg = ti.getText().toString();
cattxt.setTypeface(font);
cattxt.setText(ctg);
Button yesB = (Button) customView.findViewById(R.id.maglaro2);
yesB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GameScreen.this, Category.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
GameScreen.this.finish();
}
});
Button noB = (Button) customView.findViewById(R.id.hindi);
noB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(GameScreen.this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
GameScreen.this.finish();
}
});
mPopupWindow.showAtLocation(table, Gravity.CENTER,0,0);
}
uTxt.setText("");
}
}
});
Now my problem is I want an UNDO Button that will delete the last character on the uTxt and will change back the color of the last touched textView
Does anyone have any ideas on how to do that?
If yes leave a comment, answer, or suggestion below. TIA!
Typical solution for this problem is the usage of the command pattern (excellent for undo redo functionality).
See https://en.wikipedia.org/wiki/Command_pattern
thanks for always being available...
I have been able to populate my ListView with information i have in the database.
Now, I want setOnClickListener for the items to go to different pages. Currently, I am using a "switch(position)" construct, but it doesn't function appropriately since I can't hard code how many items would be on the ListView.
Please, kindly help me out on this one.
public class SurveyActivity extends AppCompatActivity {
ArrayList<String> list = new ArrayList<String>();
SQLiteDatabase db;
DatabaseHelper helper = new DatabaseHelper(this);
String listItem = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_survey);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Add new survey", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
PopupMenu pop = new PopupMenu(SurveyActivity.this, fab);
pop.getMenuInflater().inflate(R.menu.addsurvey_menu, pop.getMenu());
pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(), "Your choice is to " + item.getTitle().toString().toUpperCase() + " a survey form", Toast.LENGTH_LONG).show();
switch (item.getItemId()) {
case R.id.c:
//Takes admin to page where survey is created
Intent i = new Intent(getApplicationContext(), CreateSurvey.class);
startActivity(i);
case R.id.d:
//gets ready created XML forms from server
case R.id.r:
//refreshes and updates survey list from db
}
return true;
}
});
pop.show();
}
});
db = helper.getReadableDatabase();
//Cursor crs = db.rawQuery("SELECT * FROM tbNames", null);
Cursor crs=db.query("tbNames",new String[]{ "names"},null,null,null,null,null);
if(crs.moveToFirst()){
do {
listItem =crs.getString(0);
list.add(listItem);
}while(crs.moveToNext());
}
ListView surveyList = (ListView) findViewById(R.id.surveylist);
ArrayAdapter<String> aAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
surveyList.setAdapter(aAdapter);
registerForContextMenu(surveyList);
surveyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
Intent i = new Intent(getApplicationContext(), hivActivity.class);
startActivity(i);
break;
case 1:
Intent intent = new Intent(SurveyActivity.this, CreatedSurveyActivity.class);
startActivity(intent);
}
}
});
}
}
My app would allow users create multiple surveys which would show up in the LIstView.
The problem is how to set Listeners for the items dynamically too.
You can try this code
surveyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent i = new Intent(getApplicationContext(), hivActivity.class);
startActivity(i);
} else {
Intent intent = new Intent(SurveyActivity.this, CreatedSurveyActivity.class);
startActivity(intent);
}
}
});
First item will call hivActivity and all other items will call CreatedSurveyActivity. If you need to pass something to the CreatedSurveyActivity put it into the intent, like this intent.putExtra("position" position);
Maybe use setOnItemClickListener()
Posting your code would be helpful, however an approach that can often help in similar situations is:
Set a tag on each view, as you generate it. For example, the item ID, or just an incrementer.
The onClickListener will receive the view as a parameter. The tag can then be retrieved from it.
This tag can then be matched to page names, page URLs, or any other information you're trying to load.
im trying to build share dialog.
in this share dialog I have facebook,whatsapp,mail and more.
im trying the make the background of every view change on touch to indicate the toch.
so far no problem.
the problem is that I also want to let the user move is finger into other option and when he do that the previous background return to his original color and the new view background changes.
i just cant get the hover event at all, and I couldn't trigger other onTouchEvent as long as the first one is still alive.
this is my code so far:
public class customDialogFragment1 extends DialogFragment {
public customDialogFragment1() {
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
postShareUrl=getActivity().getResources().getString(R.string.servicePostShareUrl);
id=getArguments().getString("body");
postTitle=getArguments().getString("subject");
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.share_title_layout, null);
// Set title divider color
TextView txtTitle= (TextView) v.findViewById(R.id.share_title);
Typeface tf = Typeface.createFromAsset(getActivity().getResources().getAssets(),
"fonts/OpenSansHebrew-Bold.ttf");
txtTitle.setTypeface(tf);
View layout=inflater.inflate(R.layout.custom_share_layout_inner,null);
builder.setView(layout);
ImageView facebook= (ImageView) layout.findViewById(R.id.imgFaceebook);
ImageView whatsapp= (ImageView) layout.findViewById(R.id.imgWhatsapp);
ImageView more= (ImageView) layout.findViewById(R.id.imgMore);
ImageView mail= (ImageView) layout.findViewById(R.id.imgMail);
facebook.setOnClickListener(imageClickListener);
whatsapp.setOnClickListener(imageClickListener);
more.setOnClickListener(imageClickListener);
mail.setOnClickListener(imageClickListener);
List activities = getActivity().getPackageManager().queryIntentActivities(sendIntent, 0);
Context context=(Activity)getActivity();
for(int i=0;i<activities.size();i++) {
ResolveInfo appPacageName = (ResolveInfo) activities.get(i);
Log.i("pacageName", appPacageName.toString());
if (appPacageName.toString().contains("com.facebook.composer")) {
shareCheckList[0] = appPacageName;
} else if (appPacageName.toString().contains("whatsapp")) {
shareCheckList[1] = appPacageName;
} else if (appPacageName.toString().contains("mail")) {
shareCheckList[2] = appPacageName;
}
}
dialog.setContentView(layout);
Window window = dialog.getWindow();
window.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
window.setLayout(MainActivity.screenWidth-90,350);
return dialog;
}
and this is my ontouch listener :
private View.OnClickListener imageClickListener=new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT,getArguments().getString("subject"));
intent.putExtra(Intent.EXTRA_TEXT,getArguments().getString("body"));
switch (v.getId()){
case R.id.imgFaceebook:
if(!shareCheckList[0].toString().isEmpty()){
intent.setClassName(shareCheckList[0].activityInfo.packageName, shareCheckList[0].activityInfo.name);
((Activity)getActivity()).startActivity(intent);
break;
case R.id.imgWhatsapp:
intent.setClassName(shareCheckList[1].activityInfo.packageName, shareCheckList[1].activityInfo.name);
((Activity)getActivity()).startActivity(intent);
}
break;
case R.id.imgMail:
try{
intent.setClassName(shareCheckList[2].activityInfo.packageName, shareCheckList[2].activityInfo.name);
((Activity)getActivity()).startActivity(intent);
break;
case R.id.imgMore:
CustomDialogFragment2 cdf=new CustomDialogFragment2();
Bundle bundle = new Bundle();
bundle.putString("body",id);
bundle.putString("subject", postTitle);
cdf.setArguments(bundle);
cdf.show(getActivity().getFragmentManager(), "customDialogFragment2");
break;
}
}
};
If I understood right, you should use state list:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Just create xml drawable file for every ImageView, and set for each state item with source you need.
In my app I have a dialog (dialog1), containing Listview with an ArrayAdapter having 3 string items. I want to set onItemclickListener() on this list,through which I would be able to start different activities on different item click. Please Help.
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,R.layout.new_service_request,LIST));
ListView lv=getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) v).getText(),
Toast.LENGTH_SHORT).show();
// int ITEM_CLICKED = (int)getSelectedItemId();
switch(position){
case 0:
Intent intent1 = new Intent(New.this, Next.class);
startActivity(intent1);
break;
case 1:
Intent intent2 = new Intent(New.this, List.class);
startActivity(intent2);
break;
case 2:
Intent intent3= new Intent(New.this, HotCard.class);
startActivity(intent3);
break;
See if this helps
dialog1.setItems(array_of_items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//which is the item number in the list which you can use
//to do things accordingly
}
});
Not sure if this will work but, you could try within the dialog:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do stuff here
}
});
I would recommend having a go at reproducing the example from http://www.vogella.de/articles/AndroidListView/article.html and then trying to adapt it to suit your purposes (i.e. in your dialog). I usually find it easier to get a grip on the problem in a simple use case before trying to shoe-horn it into my code. So in your ListActivity, you would call
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, yourThreeStringArray));
Then instead of the call to Toast in the example, simply start your activity something like this:
Intent myIntent = new Intent(this, MyIntent.class);
startActivityForResult(myIntent, ACTIVITY_CREATE);
(Replacing MyIntent with the class of your intended action, of course.)
Good luck!
My requirement is : List all sales route & highlight default, It should be first option. If the salesperson select other option(not selected default), then popup window should come. The popup window contain one form, it talk to DB.After submit the popup window form , it return previous screen , It need to allow to go other position.
See my code
This initial list Activity:
ArrayList<Object> routeList = getWmRoute();
ArrayList<String> routhPath = new ArrayList<String>();
for(int i = 0; i<routeList.size();i++){
routhPath.add(((WMRoute) routeList.get(i)).getDescription());
}
ArrayAdapter ad = new ArrayAdapter(this,android.R.layout.simple_list_item_single_choice,routhPath);
setListAdapter(ad);
final ListView list=getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setItemsCanFocus(true);
list.setTextFilterEnabled(true);
list.setItemChecked(0,true);
list.setSelection(0);
This is listener method
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if(position !=0 ){
Bundle bundle = new Bundle();
int postion = position;
String aString = Integer.toString(postion);
bundle.putString("positon", aString);
Intent showContent = new Intent(getApplicationContext(),SalesRouteDevitionActivity.class);
// startActivityForResult(showContent,0);
startActivity(showContent);
}
}
This is my SalesRouteDevitionActivity class
array_spinner=new String[2];
array_spinner[0]="Rain";
array_spinner[1]="Floods";
Bundle bundle = this.getIntent().getExtras();
param1 = bundle.getString("param1");
Spinner s = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, array_spinner);
s.setAdapter(adapter);
final Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(SalesRouteDevitionActivity.this, "Beep Bop", Toast.LENGTH_SHORT).show();
Intent showContent = new Intent(getApplicationContext(),SalesRouteActivity.class);
Bundle bundle = new Bundle();
bundle.putString("position", param1);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
startActivity(showContent);
}
});
This is my manifest file
<activity android:theme="#android:style/Theme.Dialog" android:name=".SalesRouteDevitionActivity"
android:label="Sales Route Diviation">
</activity>
After finish pop window work , how we can go to previous activity that particular place?
Please help me...
Thanks in advance
from wherever you start the popup window call it using intent throught call this startActivityForResult(your_intent,requestCode)
it will start the activity and in the popup activity do like this way
Bundle bundle = new Bundle();
bundle.putString("position", param1);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
so the popup will finish his activity and go back to previous activity where it will invoked with the result
and in your activity override the onActivityResult like this way
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==1){
if(resultCode==RESULT_OK)
Toast.makeText(this, "SUCCESS", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Fail", Toast.LENGTH_LONG).show();
}
}
You could create a global variable for the position of the selected item. Then when you return to the previous activity, you call listView1.smoothScrollToPosition(int position).
Do you want listview focus on a position which is not default?If so, you only need return intent with position and at onActivityForResult() you set focus at that position.If you have any problems, please add my yahoo or skype:fsoft_duonghv and we discuss .
Instead of starting a new activity for a popup window you can create a AlertDialog. You can call dismiss() to return your activity then.