howto fire onCreateContextMenu or howto have a contextMenu without any View - android

I was wondering if it's possible to have a contextMenu without any view..?
Or any Dialog, acting simply like a contextMenu (a list of clickable items in fact)..?
I can explain: on the first use of the app, a pop-up (ContextMenu) list all option modes.
The actual trick is a button, registered for the ContextMenu and the firing is done by button.performLongClick()...
I don't want to have that button anymore, but I still want the ContextMenu
Any idea?
Thanks in advance,
jo

Is that what you want?:
public class MyActivity extends Activity{
static final int MY_DIALOG_ID = 0;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// Sets the activity layout
setContentView(R.layout.my_activity_layout);
showDialog(MY_DIALOG_ID);
}
#Override
protected Dialog onCreateDialog(int dialogID) {
Dialog d;
switch(dialogID){
case MY_DIALOG_ID:
//CREATE YOUR DIALOG HERE
break;
}
return d;
}
}

Related

Is it possible to popup some window above the mainActivity?

I have some listView on my mainActivity - the listView contain names of people.
I want to make some window popup that will contain all 'more info' about someone that was click on him.
I mean that if the user click on some person form this list - i want to popup some window above the mainActivity and show more information about that person that was the one who the user click on him
I can't find a way to create the popup window
( AlertDialog is not what i need .. i need dialog that i can edit fully and set data as i want )
yes it's possible
extend Dialog class and make a custom view
public class MyDialog extends Dialog {
YourParams yourParams;
Context context;
public MyDialog(Context context, YourParams yourParams) {
super(context);
this.context = context;
this.yourParams = yourParams;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_sample);
//
// some code ...
//
}
}
then call it like this
MyDialog dialog = new MyDialog(MainActivity.this,yourParams);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
To follow material design guidelines, I think you can make use of Bottom Sheets, like here on that middle screen:
There are many libraries that will help you with this, for example https://github.com/Flipboard/bottomsheet

how to show a custom dialog box when an activity start

I want to show a custom dialog box when I start my first activity without using a button. I try to search but I'm not finding the proper solution what I really want to do. Many of them are using onClick Listener to achieve that the scenario. Below image is showing an activity with a dialog box, that's what I'm looking for but without using onClick Listener.
How can we implement without using onClick Listener?
Any code contained in a click listener also works elsewhere in the class (unless you use the view that's clicked)
Create the dialog in onCreate. It will open immediately when the activity is started
In your main Activity oncreate method:
createCustomizeDialog();
now create this method outside of oncreate:
private void createCustomizeDialog() {
final AlertDialog.Builder builder=new AlertDialog.Builder(this);
LayoutInflater inflater = getActivity().getLayoutInflater();
#SuppressLint("InflateParams") final View alertLayout = inflater.inflate(R.layout.customize_dialog, null);
Button submit=(Button)alertLayout.findViewById(R.id.sButton);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
builder.setView(alertLayout);
alertDialog=builder.create();
//noinspection ConstantConditions
alertDialog.show();
}
Just try this way
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("TADAAAA!").create().show();
}
If you want to show the dialog only for the FIRST launch of this activity, you should put the code for your dialog in onCreate method of this activity, if it should be done for EVERY launch of this activity - then in onStart() method.

ListView after click and back List is empty

I have a layout with a ListView and ImageView. Initially, ImageView is invisible. I have a list, when an Item is clicked, it shows the Image of that item by setting ImageView and making it visible and listView is invisible, so far everything is ok. From there, onBackPressed() I want to see my listview again, I override onBackPressed(), to make my image view invisible and listview visible. However, when I select an item and see the image of it then press back, a blank activity comes, not my listView. I don't want to call the activity again, what should I do? What is wrong about listView? I tried to call invalidate() and invalidateViews(), setting adapter again, but they aren't woring.
Actually I'm filing adapter in the onResume of the activity, here is the code:
public class MyViewActivity extends Activity{
ListView imageList;
CustomImageListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_list_view);
//initialization of image array list etc
}
#Override
public void onResume(){
super.onResume();
imageList = (ListView) findViewById(R.id.listView);
adapter = new CustomImageListAdapter(this,R.layout.image_item, imageNames);
imageList.setAdapter(adapter);
imageList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> adapter, View view, int i,
long l) {
// TODO Auto-generated method stub
if(imageNames.get(i) != null) {
onImageSelected(i);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.image_list_view, menu);
return true;
}
public void onImageSelected(int position) {
...
}
#Override
public void onBackPressed() {
if (img.getVisibility() == ImageView.VISIBLE){
// img.setVisibility(ImageView.INVISIBLE);
// imageList.invalidateViews();
// imageList.setVisibility(ListView.VISIBLE);
// Here actually I want to use the upper part that I commented, but it didn't work, I have to call the same activity again to see my listview
Intent i = new Intent(MyViewActivity.this, MyViewActivity.class);
startActivity(i);
finish();
}
else {
....
}
return;
}
private class CustomImageListAdapter extends ArrayAdapter<String>{
...
}
As long as I can guess, the problem is caused by
img.setVisibility(ImageView.INVISIBLE);
You can try to use View.GONE instead.
The difference is clearly stated in android api's doc and I quoted:
/**
* This view is invisible, but it still takes up space for layout purposes.
*/
public static final int INVISIBLE = 0x00000004;
/**
* This view is invisible, and it doesn't take any space for layout
*/
public static final int GONE = 0x00000008;
By the way, I will recommend to use fragments to do your job. Specifically, a fragment A to present the listview, and when an item is clicked, switch to fragment B which contains the content. When Back is pressed, just let the backstack do its job.
Intent i = new Intent(MyViewActivity.this, MyViewActivity.class);
i.putExtra("currentObjectID",objectID);
startActivity(i);
finish();
Why are you traversing within the same activity? You should understand the use of intents ,If you want to stay within the same activity , Just set a flag for currentObjectID instead of put Extra..

ListView assistance

I'm working on a project at school, and I need help implementing ListViews properly. I have to do the Settings and Help sections on the project and as of now, I can display what's on my list and it displays Toast messages of the selected item when I click it. My question is, how can I create and show the content that is inside that specific item? For example, I have an option "Edit Password" and that when I click it, it should display my "Edit Password" screen. This applies to both my sections.
This is what I have so far. It's basically the android ListView tutorial but I added my options on there. My question is, when I click one of the options on my list, how can I display its specific details? Like what I said before, if I click "Edit Password", I want it to go to a "Edit Password" screen. Or on Help, if I click let's say, "Credits", I want it direct me to the Credits page.
public class Settings extends ListActivity {
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.settings, SETTINGS));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
static final String[] SETTINGS = new String[]
{"Set Refresh Rate", "Edit Password", "Delete Account"};
}
When you extend ListActivity you already have the OnItemClickListener implemented, you should override the method onListItemClick. In that method you should use an Intent to get to a new Activity where you will display the stuff you want:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(this, SecondActivityName.class);
i.putExtra("pos", position); //pass the position of the clicked row so we know what to show.
startActivity(i); // let's go to the other activity
}
SeconActivityName is an Activity that you should create and where you would show the other screen you want(remember to add the activity to the manifest file!):
public class SecondActivityName extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
int rowPosition = i.getIntExtra("pos", -1); //we now have the row's position that was clicked in the other activity.
// based on that you show the screen you want for example after a switch
switch (rowPosition) {
case -1:
//this is the default value, something has gone terrible wrong
//finish the activity and hide:
finish();
break;
case 0:
//row 0 was clicked so show the "Set Refresh Rate" screen
setContentView(R.layout.refresh_rate__screen);
break;
//do the same for the other rows;
//...
This will work if the screens for the various settings are not that different. If they are you'll probably have to implement a different activity for each one and start the appropriate activity in the onListItemClick based on the position parameter.

Use single xml layout for multiple activities with different datas

I know this is a very basic question, however as a newbie i cant get to work around it.
So, I want to have multiple activities to use same the xml layout(consist for example of 1 imagebutton, and multiple textviews with different IDs). Now, for every activity, I want them to view the same layout but override the views with data unique to every activity. What is the best way to do this? And also, the imagebutton should open different URLs in a video player(youtube links).
And can somebody tell me what is the most practical way to learn android programming?
UPDATE
This is my current code:
public class TemakiActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contentviewer);
}
}
For example I have a textview with ID "descriptionviewer", and a button with ID "videolink", now, how do you code those in?
You can share the same layout file and the set the attributes for views in the onCreate(..) method of each activity.
If you want a different URL to open for each image button you could set it at runtime as follows
public void onCreate(Bundle b) {
Button button =(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//different action for each activity
}
});
}
Yes you can! I had multiple activities inflate the same layout but they save different shared preferences.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.same_layout);
TextView urlDesc = (TextView)findViewById(R.id.descriptionviewer);
urlDesc.setText("url_1"); //now in other activities-- urlDesc.setText("url_2");
ImageButton aButton = (ImageButton)findViewById(R.id.videolink);
aButton.setOnClickListener(aButtonListener);
}
private OnClickListener aButtonListener = new OnClickListener() {
public void onClick(View v) {
// go open url_1 here. In other activities, open url_x, url_y, url_z
finish();
}
};
Same code just swapping the text you want to set for the TextView and url to open in OnClickListener(). No more to change.

Categories

Resources