I need create activity with navigation drawer only with java without xml. but this way doesn't work for me. Is it possible to create and add to app Navigation Drawer without using xml? Every example i saw had an additional xml file with layout. I searched very much but I did not get any results. Please help me. in this way navigation drawer always open on contentLayout and not moving with finger or swiping !
my activity code:
import androidx.drawerlayout.widget.DrawerLayout;
public class LaunchActivity extends Activity {
private Context context;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
//DrawerLayout
drawerLayout = new DrawerLayout(context);
drawerLayout.setLayoutParams(new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.MATCH_PARENT, DrawerLayout.LayoutParams.MATCH_PARENT));
//ContentLayout
LinearLayout contentLayout = new LinearLayout(context);
contentLayout.setOrientation(LinearLayout.VERTICAL);
contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
//CustomNavigationView
RelativeLayout drawerView = new RelativeLayout(context);
drawerView.setLayoutParams(new RelativeLayout.LayoutParams(AndroidUtilities.dp(280), RelativeLayout.LayoutParams.MATCH_PARENT));
drawerView.setGravity(Gravity.START);
//Add View to ContentLayout
TextViewFont textView = new TextViewFont(context);
textView.setText("=");
textView.setTextColor(Color.BLACK);
textView.setTextSize(20);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.openDrawer(Gravity.START);
}
});
contentLayout.addView(textView);
//Add View to CustomNavigationView
RelativeLayout drawerHeaderView = new RelativeLayout(context);
drawerHeaderView.setBackground(getResources().getDrawable(R.drawable.drawer_header));
drawerHeaderView.setGravity(Gravity.BOTTOM);
drawerHeaderView.setLayoutParams(new RelativeLayout.LayoutParams(AndroidUtilities.dp(280), AndroidUtilities.dp(170)));
ImageView imageView = new ImageView(context);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.user));
imageView.setBaselineAlignBottom(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(AndroidUtilities.dp(90), AndroidUtilities.dp(90));
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
drawerHeaderView.addView(imageView, params);
drawerView.addView(drawerHeaderView);
//Add All to DrawerLayout
drawerLayout.addView(contentLayout, new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.MATCH_PARENT, DrawerLayout.LayoutParams.MATCH_PARENT));
drawerLayout.addView(drawerView, new RelativeLayout.LayoutParams(AndroidUtilities.dp(280), RelativeLayout.LayoutParams.MATCH_PARENT));
setContentView(drawerLayout);
}
}