Long-press on item message to change layout - android

Long press
When I long-click on an item of the message, the item will display and the layout changes like the picture. I want to make this , but i don't have a keyword to find this solution. I need a keyword or some example to make it.

Many ways you can do. I am going to share one example.
Implement View.OnLongClickListener as follows
private void setupLongPress() {
imageButton.setOnLongClickListener(new View.OnLongClickListener(){
#Override
public boolean onLongClick(View v){
// here your staff
// we added dialog method here as follows
createPreviewDialog();
return false;
}
});
}
Now use LayoutInflater to inflate new layout as a popup windows
private Dialog createPreviewDialog() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_preview, null);
LinearLayout closeButton = view.findViewById(R.id.close);
closeButton.setOnClickListener (new View.OnClickListener (){
#Override
public void onClick ( View view ) {
dismiss();
}
});
View okButton = view.findViewById(R.id.ok);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
// here your staff
}
});
builder.setView(view);
return builder.create();
}

Related

Disable click after clicking the textview

I have a TextView and I put a OnClickListener on this TextView. I use this action to load custom view onto a LinearLayout.
But when I click on this TextView twice, custom view is repeating on the LinearLayout. I clear all custom views on this LinearLayout before I load new custom views on to this LinearLaout.
This is my OnClickListener on TextView,
TextView rejectedTitleTextView = (TextView) findViewById(R.id.roster_menu_rejected_title);
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
rejectedTitleTextView.setBackgroundColor(getResources().getColor(R.color.acceptedPurpleColour));
newTitleTextView.setBackgroundColor(getResources().getColor(R.color.defaultBlack));
acceptedTitleTextView.setBackgroundColor(getResources().getColor(R.color.defaultBlack));
locationLinearLayout.removeAllViews();
rosterBottomLayout.setVisibility(View.GONE);
Log.d("CHECK_ACTION"," REJECTED_TEXT_VIEW ");
InternetConnectivity internetConnectivity = new InternetConnectivity();
final boolean isConnectedToInternet = internetConnectivity.isConnectedToInternet(context);
if(isConnectedToInternet==true) {
try {
Thread.sleep(1300);
} catch (Exception e) {
e.printStackTrace();
}
getDataFromServer("REJECTED");
}else{
Snackbar.make(mainView, "No Internet Connection", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
});
getDataFromServer("REJECTED");
is the method which I used to load custom view onto this LinearLayout.
How can I prevent this issue ?
Have any ideas ?
Inside onclickListener put
rejectedTitleTextView.setClickable(false);
and once finish your functionality make it as true because u need to click for next time .
rejectedTitleTextView.setClickable(true);
Inside setOnclickListener try below code:-
textView.setClickable(false);
Try this
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mSpinner.setEnabled(false);
mSpinner.postDelayed(new Runnable() { #Override public
void run() {
mSpinner.setEnabled(true); }
}
// do your stuff here
});
You can maintain boolean value like this
boolean isClick=false;
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isClick)
{
//do your Stuff on onCLick
isClick=true;
}else
{
//leave it blank if you do not want to do anything second time
}
}
});

Android select spinner item into alert dialog and text button disappear

I have a listview with a series of elements.
The click on an item of listview shows me a custom dialog.
In custom dialog I have a layout with:
A spinner
Two buttons (OK / ANNULLA)
This is the normal situation:
When I select the spinner, he shows a list of items.
When I select an item from the spinner, the text that was present on the buttons disappear in this way:
ps: this does not happen on Android 6.0, but it happens in the lower versions (such as 5.0)
The Code:
public void showDialogTagAssociation (Activity activity, Handler handler,
String msg, final MyOperator elemento, final BluetoothDevice device,
final int position){
mHandler = handler;
//-----------------------------------------------------
// DIALOG
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
//-----------------------------------------------------
//---------------------------------------------------------------------
// LAYOUT
dialog.setContentView(R.layout.alert_dialog_custom_tag);
**// Spinner element
spinner = (Spinner) dialog.findViewById(R.id.spinner);**
// Spinner click listener
**spinner.setOnItemSelectedListener(new OnSpinnerItemClicked());**
//---------------------------------------------------------------------
//----------------------------------------------------------------------
// BUTTON OK
dialogButtonOK = (Button) dialog.findViewById(R.id.acd_btn_ok);
dialogButtonOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Devo assegnare il tag al nome....");
Log.d(TAG, "Nome: " +tmpNome+"\n" +
"TAG: "+device.getName()+" - "+device.getAddress());
dialog.dismiss();
}
});
// BUTTON ANNULLA
dialogButtonNO = (Button) dialog.findViewById(R.id.acd_btn_no);
dialogButtonNO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
//----------------------------------------------------------------------
dialog.show();
}
**private class OnSpinnerItemClicked implements android.widget.AdapterView.OnItemSelectedListener {**
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//NOME
tmpNome = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
Thanks for the future help
Remove that piece of code:
spinner.setOnItemSelectedListener(new OnSpinnerItemClicked());
and the thing which you want will still work.

DialogFragment - Where to access layout components [i.e. editText.setText("")]

after doing a lot of research and trying (i think) every lifecycle method i could find, i´m really stuck:
What i want: Basically i have this DialogFragment ("ProfileUsernameDialogFragment")which should present my layout file ("fragment_profile_header_username_dialog").
in this layout file i have some TextViews and some EditTexts. I want to set the text of these EditTexts to some value i obtain in my programm, so setting it beforehand is not possible.
What i have:
public class ProfileUsernameDialogFragment extends DialogFragment {
...
#Override
public void onActivityCreated(Bundle savedInstanceState) {
EditText usernameText = (EditText) this.getView().findViewById(R.id.username_field);
usernameText.setText("TEST");
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.d(AppController.LOG_STRING, this.getClass().toString() + " - onCreateDialog - START");
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
View view = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_profile_header_username_dialog, null);
builder.setView(view);
builder.setPositiveButton(SAVE_SETTINGS, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
// Tried this too:
// EditText usernameText = (EditText)
// ProfileUsernameDialogFragment.this.getView().findViewById(R.id.forename_label);
// usernameText.setText("TEST");
}
});
builder.setNegativeButton(ABORT_ACTION, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
builder.setTitle(CHANGE_NAME_SETTINGS);
Dialog dialog = builder.create();
return dialog;
}
}
What i get: is a NullPointerException, no matter which position for the findViewById(R.id.username_field) i tried.
(I also tried onCreate and some other methods, i start thinking i might have another problem than the code location, but i really hav no clue) I tried to follow at least 6 similar questions about NullPointers in DialogFragments so i guess i need advanced help ^^
i should add i´m completely new to android so please be nice, but convention tipps etc. are very welcome! thx in advance :)
Following is what you need:
mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(final DialogInterface dialog) {
final Button b = mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
v.findViewById(R.id...)
}
});
}
});
});
}
});
Another option in addition to #David's answer is to use the onStart() method.
It looks like you were thinking along these lines in your question, except you used the onActivityCreated() method.
So for example:
#Override
public void onStart()
{
super.onStart();
EditText usernameText = (EditText) this.getDialog().findViewById(R.id.username_field);
usernameText.setText("TEST");
}
Android documentation has good example
So, if You wont set text in Your layout You need set it in onCreateView() method, for example:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup cont,
Bundle savedInstanceState) {
// Inflate the layout to use as dialog or embedded fragment
View view = inflater.inflate(R.layout.app_info, cont, false);
TextView title = (TextView)view.findViewById(R.id.dialog_header);
TextView content = (TextView)view.findViewById(R.id.dialog_content);
Button button = (Button)view.findViewById(R.id.dialog_button);
title.setText(this.dialogTitle);
content.setText(this.dialogContent);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
getDialog().dismiss();
}
});
return view;
}

how to make a click event on a textview of child of a custom listview in android

Basically I'm new to Android and don't know much about it. I'm making a quiz program in which I'm using custom ListView with 5 custom TextViews, one for question and other 4 for options. My problem is that I want the TextView as clickable as well as the LisView as choice mode as single. That is if I click one text all other TextViews should become unclickable. My problem is whenever I click on a TextView in the child layout, only the outer layout, that is the item of the ListView get selected.
here is the screenshot of the my listview
https://picasaweb.google.com/108429569548433380582/Android?authkey=Gv1sRgCJ3kxJz7tLvaTg#5783846428648608706
You can do it in two ways:
1. Either by directly using onClickListener like this:
textView.setOnClickListener(new View.OnClickListener(
public void onClick(View arg0) {
// Do anything here.
}
});
OR
2. In XML file, in declaration of <TextView /> add one more attribute as:
android:onClick="onClickTextView"
and in yout activity, add this function:
public void onClickTextView(View view) {
// Do anything here.
}
UPDATE:
Use following code to get click event on TextView:
// Click event for single list row
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv = (TextView) (findViewById(R.id.title));
if (tv != null) {
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CLICKED",
Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(MainActivity.this, "TV not found",
Toast.LENGTH_LONG).show();
}
}
});
Try this :
When you select one textview the other three will be unclickable
final TextView texta = (TextView) findViewById(R.id.text_a);
final TextView textb = (TextView) findViewById(R.id.text_b);
final TextView textc = (TextView) findViewById(R.id.text_c);
final TextView textd = (TextView) findViewById(R.id.text_d);
texta.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
textb.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textb.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textc.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textd.setClickable(false);
}
});
textd.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textc.setClickable(false);
}
});
Assume you extend BaseAdapter to set the listview content ->
Open a TextView listener and settag of the current holder position , and perform your operation in the onclick method.
It's the default behavior of a ListView. Only one could be clickable: either the list row or the items inside the row.
Eg: if the row item is a textView(as in your case) the list row will be clickable but if the row item is a button then the the list row will not be clickable. Same is the case if you make TextView as clickable.
For your requirement the better approach would be to use RadioGroup (instead of multiple text view and disabling and enabling them).
You should use a custom layout for you list item with a TextView for question and a RadioGroup for options.
Layout could be something like this :
Follow these links for reference:
for listView
for RadioGroup
I hope this will help
Thanks for Shrikant and adam for there help Sorry and i appologize for a very late response.
either use this in adapter class as by Shrikant:
textViewa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do anything here.
}
});
textViewb.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Do anything here.
}
});
//and so on...
// or better to use ViewHolder holder; for these type of listviews;
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do what you want to do.
// for my i have to call a method in my parent activity. so in constructor of adapter, I passed the activity and then typecasted it like
ParentActivity parent = (ParentActivity) activity;
parent.chosenAnswer(view.getId());
// then in chosenAnswer(int id) in parentActivity use a switch case for the same logic as in Adam's answer.
// OR
//you can write like this too..
switch (view.getId()) {
case R.id.textViewa:
break;
case R.id.textViewb:
break;
case R.id.textViewc:
break;
case R.id.textViewd:
break;
}
}
};

Gridview Button Adapter context menu

I have a phrasebook, with the ability to save the sample to SD. I use a Gridview set up with the following code in place for the button adapter:
public View getView(int position, View convertView, ViewGroup parent) {
try {
final Sample sample = board.getSamples().get(position);
if (sample != null) {
Button button = new Button(context);
button.setText(sample.getName());
button.setTextColor(Color.WHITE);
button.setTextSize(12);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
context.play(sample);
}
});
// TODO Implement this correctly.
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
return context.saveToSD(sample);
}
});
return button;
}
} catch (IndexOutOfBoundsException e) {
Log.e(getClass().getCanonicalName(), "No sample at position "
+ position);
}
return null;
}
I am looking to integrate a context menu here on a Long press, to give the option of where to save the sample. I don't seem to be able to register the button for the context menu within this method (ie registerForContextMenu(button), as it gives me errors.
I am a bit stumped here, any pointers would be a great help.
Thanks
I take it that this is an old post but I came across it today as I was looking for an answer on the same topic. Like the question here I have a grid of items and wanted to show a context menu on long click.
I am not using a contextmenu but instead I am using an AlertDialog.
gridview.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id)
{
showOptionsMenu(position);
return true;
}
});
public void showOptionsMenu(int position)
{
new AlertDialog.Builder(this)
.setTitle("test").setCancelable(true).setItems(R.array.myOptions,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
//take actions here according to what the user has selected
}
}
)
.show();
}
Hope this helps.

Categories

Resources