How to adapt the width of a LinearLayout in a RelativeLayout programmatically? - android

I have a main RelativeLayout object and I want add a LinearLayout object into this,but also by setting the LayoutParams of LinearLayout,the elements inside it,align to left,while I would like that the last element is attached at the right end of the window.
This my first test code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
setContentView(mainLayout);
LinearLayout Bottom = new LinearLayout(this);
Button scale = new Button(this);
scale.setText("Scale");
Button attach = new Button(this);
attach.setText("Attacch");
TextView text = new TextView(this);
text.setText("Something");
Bottom.addView(scale);
Bottom.addView(text);
Bottom.addView(attach);
mainLayout.addView(Bottom);
}
}
and this is the result:
if I edit the code in this way:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar= getActionBar();
RelativeLayout mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
setContentView(mainLayout);
LinearLayout Bottom = new LinearLayout(this);
Button scale = new Button(this);
scale.setText("Scale");
Button attach = new Button(this);
attach.setText("Attacch");
attach.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TextView text = new TextView(this);
text.setText("Something");
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
Bottom.addView(scale);
Bottom.addView(text);
Bottom.addView(attach);
mainLayout.addView(Bottom);
}}
I get this:
as you can see the attacch button disappears...
In short,I want center text and align attach button to the right

// try this way
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
RelativeLayout mainLayout = new RelativeLayout(this);
LinearLayout Bottom = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
Button scale = new Button(this);
LinearLayout.LayoutParams scaleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
scaleParams.gravity=Gravity.CENTER;
scale.setText("Scale");
Button attach = new Button(this);
LinearLayout.LayoutParams attachParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
attachParams.gravity=Gravity.CENTER;
attach.setText("Attacch");
TextView text = new TextView(this);
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
textParams.gravity=Gravity.CENTER;
text.setText("Something");
Bottom.addView(scale,scaleParams);
Bottom.addView(text,textParams);
Bottom.addView(attach,attachParams);
mainLayout.addView(Bottom,layoutParams);
setContentView(mainLayout,new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT));
}

Related

Dynamic views not displaying in fragment: android

I have a ScrollView with a child LinearLayout. I am programmatically adding TextViews and NumberPickers to the LinearLayout through a separate View method. However the dynamic objects do not display when the tab containing the ScrollView is clicked.
Here is my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.activity_settings_tab, container, false);
super.onCreate(savedInstanceState);
myDb = new DatabaseHelper(getContext());
rootview.findViewById(R.id.scroll_config);
viewFunds();
return rootview;
}
Here is the separate method I have mentioned that dynamically adds objects to the LinearLayout:
public View viewFunds(View rootview) {
ScrollView scrollView = new ScrollView(getActivity());
scrollView.findViewById(R.id.scroll_config);
LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.findViewById(R.id.ll_config);
//final View linearLayout = getActivity().findViewById(R.id.ll_config);
linearLayout.removeAllViews(); //clear layout first - LINE WITH ISSUE
linearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//create dynamic objects inside scrollview and dynamic linear layout - horizontal
for (int i = 0; i < res2.getCount(); i++) {
LinearLayout llh = new LinearLayout(getActivity());
llh.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llh.setLayoutParams(lp_llh);
linearLayout.addView(llh);
NumberPicker numberPicker = new NumberPicker(getActivity());
numberPicker.setMinValue(0);
numberPicker.setMaxValue(100);
LinearLayout.LayoutParams lp_np = new LinearLayout.LayoutParams(70, LinearLayout.LayoutParams.WRAP_CONTENT);
numberPicker.setLayoutParams(lp_np);
numberPicker.setGravity(Gravity.CENTER_HORIZONTAL);
//showMessage("value",res2.getString(3));
numberPicker.setValue(Integer.parseInt(res2.getString(2))); //
TextView textView = new TextView(getActivity());
textView.setText( /*textArray[i] + " " +*/ res2.getString(1));
llh.addView(textView);
linearLayout.addView(numberPicker);
}
return scrollView;
}
What am I doing wrong here?
you are using fragment u have to find view with reference of fragment root view also your below statement doing nothing i will advise u to go through android basic components
rootview.findViewById(R.id.scroll_config);
to solve problem do following
public View viewFunds(View rootview) {
ScrollView scrollView = (ScrollView)rootview.findViewById(R.id.scroll_config);//considering scroll_config is scroll view in xml
LinearLayout linearLayout =(LinearLayout)rootview.findViewById(R.id.ll_config);//considering ll_config is LinearLayout in xml
linearLayout.removeAllViews(); //clear layout first - LINE WITH ISSUE
linearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//create dynamic objects inside scrollview and dynamic linear layout - horizontal
for (int i = 0; i < res2.getCount(); i++) {
LinearLayout llh = new LinearLayout(getActivity());
llh.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llh.setLayoutParams(lp_llh);
linearLayout.addView(llh);
NumberPicker numberPicker = new NumberPicker(getActivity());
numberPicker.setMinValue(0);
numberPicker.setMaxValue(100);
LinearLayout.LayoutParams lp_np = new LinearLayout.LayoutParams(70, LinearLayout.LayoutParams.WRAP_CONTENT);
numberPicker.setLayoutParams(lp_np);
numberPicker.setGravity(Gravity.CENTER_HORIZONTAL);
//showMessage("value",res2.getString(3));
numberPicker.setValue(Integer.parseInt(res2.getString(2))); //
TextView textView = new TextView(getActivity());
textView.setText( /*textArray[i] + " " +*/ res2.getString(1));
llh.addView(textView);
linearLayout.addView(numberPicker);///chek in which layou you want to add numberPicker
}
}
and in oncreateview
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.activity_settings_tab, container, false);
super.onCreate(savedInstanceState);
myDb = new DatabaseHelper(getContext());
viewFunds(rootview);
return rootview;
}
also go through android training https://developer.android.com/training/index.html

How to design text view in android without layout use only java class code

I am designing an android application where I have not need to create any layout xml file, but create over the Java code itself. I try with this way:-
public class MainActivity extends Activity{
TextView tb;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setBackgroundResource(R.drawable.ch);
}
}
Use this code, this will work for you
public class Amit extends Activity{
TextView tb;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setBackgroundResource(R.drawable.ch);
tb = new TextView(Amit.this);
tb.setText("hallo hallo");
tb.setId(5);
tb.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(tb);
}
Use TextView like this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText("Here is your textview");
ll.addView(tv);
}
You may try this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView tv1 = new TextView(this);
tv1.setText("HELLO");
ll.addView(tv1);
setContentView(ll);
}
Try this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText("Test");
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// Setting the parameters on the TextView
tv.setLayoutParams(lp);
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(tv);
// Setting the RelativeLayout as our content view
setContentView(relativeLayout, rlp);
}
}

Centering ProgressBar Programmatically in Android

I'm trying to center a ProgressBar programmatically using the following:
ViewGroup layout = (ViewGroup) findViewById(android.R.id.content).getRootView();
progressBar = newProgressBar(SignInActivity.this,null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);
The size setting seems to work okay, but the ProgressBar doesn't center in the existing layout (defined by xml with a relative layout). Is there something obviously wrong here?
The XML is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".test"
android:typeface="monospace">
</RelativeLayout>
i.e. it's just an empty relative layout to test with and see if I can get it to programmatically add a progress bar.
Thanks.
I wrote a class based on #DharmarajRupakheti's answer:
public class ProgressBarHandler {
private ProgressBar mProgressBar;
private Context mContext;
public ProgressBarHandler(Context context) {
mContext = context;
ViewGroup layout = (ViewGroup) ((Activity) context).findViewById(android.R.id.content).getRootView();
mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
mProgressBar.setIndeterminate(true);
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout rl = new RelativeLayout(context);
rl.setGravity(Gravity.CENTER);
rl.addView(mProgressBar);
layout.addView(rl, params);
hide();
}
public void show() {
mProgressBar.setVisibility(View.VISIBLE);
}
public void hide() {
mProgressBar.setVisibility(View.INVISIBLE);
}
}
Usage:
mProgressBarHandler = new ProgressBarHandler(this); // In onCreate
mProgressBarHandler.show(); // To show the progress bar
mProgressBarHandler.hide(); // To hide the progress bar
If you want to do it programatically you can do it like below:
RelativeLayout layout = new RelativeLayout(this);
progressBar = new ProgressBar(SignInActivity.this,null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);
setContentView(layout);
Use your coding in this style, this gives the result you want.
ViewGroup layout = (ViewGroup) findViewById(android.R.id.content).getRootView();
progressBar = new ProgressBar(thisActivity,null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout rl = new RelativeLayout(thisActivity);
rl.setGravity(Gravity.CENTER);
rl.addView(progressBar);
layout.addView(rl,params);
You don't need the XML. You can create the view all by code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
ProgressBar pb = new ProgressBar(this, null,
android.R.attr.progressBarStyleLarge);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
pb.setLayoutParams(params);
layout.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
layout.addView(pb);
setContentView(layout);
}

Create and Add TextView Progmmatically in android

I am trying to add TextView progmmatically into may RelativeLayout. it does not working.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rl = (RelativeLayout) findViewById(R.layout.activity_main);
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello World");
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rl.addView(tv);
setContentView(R.layout.activity_main);
}
The call to setContentView(R.layout.activity_main) will reinflate your layout, overriding the addition you made before it. Move it up, so it's the first thing after the super call. Something like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.layout.activity_main);
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello World");
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rl.addView(tv);
}
You should call setContentView(R.layout.activity_main) right after super.onCreate.
I've did this:
I went to main_activity.xml and added an attribute android:id="#+id/activity_main" to RelativeLayout.
Edited code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//RelativeLayout rl = (RelativeLayout) findViewById(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_main);
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello World");
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rl.addView(tv);
}
But still, the textView will overlap other generated items on RelativeView. Maybe you should consider using LinearLayout with vertical orientation instead.

Adding Views to XML layout in Java

I already have an XML layout with some buttons in it, and now i want to add a textview to the same layout, but in my Java class. I don't get any errors until the "addView" line. I would also appreciate it if someone could tell me a better way to add onto a pre-existing XML layout in Java.
public class MyActivity extends Activity{
TextView textview;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout=(RelativeLayout)findViewById(R.id.mylayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParam(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textview=new TextView(this);
textview.setId(16);
textview.setText("Help");
layout.addView(textview, params);
setContentView(layout);
}
Your code should be like this, since you already have xml layout, you should first setContent to xml layout and then add new view to the Relativelayout.
public class MyActivity extends Activity{
TextView textview;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file_name);
layout=(RelativeLayout)findViewById(R.id.mylayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParam(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textview=new TextView(this);
textview.setId(16);
textview.setText("Help");
layout.addView(textview, params);
}

Categories

Resources