i hope you are fine. im new in java, and what i want is to add any view, i determine it using java code only, i did that in dialog, i added edit text to alert dialog using this code :
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_TRADITIONAL);
alert.setTitle("Title");
final EditText myedit = new EditText(MainActivity.this);
myedit.setHint("Type something");
myedit.setLayoutParams
(new LinearLayout.LayoutParams
(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
final AlertDialog dialog = alert.create();
dialog.show();
and this work 100% with me, i got edittext in the Dialog, like this :
and now i can't get another view by using this way in the MainActivity view:
for example this code here i tried it without get anything :
final LinearLayout linear1 = new LinearLayout(this);
linear1.setOrientation(LinearLayout.HORIZONTAL);
linear1.setBackgroundColor(Color.white);
LinearLayout.LayoutParams layoutForInner = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linear1.setLayoutParams(layoutForInner);
final ListView listview1 = new ListView(this);
listview1.setLayoutParams
(new LinearLayout.LayoutParams
(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
linear1.addView(listview1);
please Help me solving this, if it is not possible please give me alternative to this way, for adding/creating views from .java only without need .xml
For This to work you must use the Dialog insted of AlertDialog class.
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.your_contentview);
After defining dialog you must define views in your dialog
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, );
LinerLayout layout = new LinearLayout(context, params);
dialog.addView(loadMsg, params);
final ListView listview1 = new ListView(this);
listview1.setLayoutParams (new LinearLayout.LayoutParams (android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
linear1.addView(listview1);
dialog.setView(layout);
dialog.show();
So to sum it up //
1.define a dialog
2.make linear layout in it
3.add listview in it. call the set view method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.