0

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 : ScreenShot

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

General Grievance
  • 4,555
  • 31
  • 31
  • 45
HishamKanon
  • 25
  • 1
  • 6
  • share your full java code – Wini Oct 06 '20 at 12:41
  • brother, all what i want is how to create linear, buttons, views...ect, programatically with code java only like this : 'myedit.setLayoutParams (new LinearLayout.LayoutParams (android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));' i want to creat views in my Home Screen MainActivity – HishamKanon Oct 06 '20 at 12:44
  • try this brother--> https://stackoverflow.com/a/6216705/12553303 – Wini Oct 06 '20 at 12:50

1 Answers1

0

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.

raj kavadia
  • 926
  • 1
  • 10
  • 30
  • thanks so much to answer, just tell me how to add view to home screen "MainActivity", not dialog but a listview, should i follow your steps ? – HishamKanon Oct 06 '20 at 12:37