I create some views programmatically, but on some devices they are displayed wrong. I have Layout in which i programmatically add spinner. This is my xml:
container - it's layout where i create spinner (programmatically)
spinnerL - here i add spinner
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#26ffffff"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/spinnerL"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_weight="0.2"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="#+id/spinnerOpen"
android:layout_width="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="0.8"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerImage"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
Here i create and add the spinner:
//here i set margins
View linearLayoutG = convertView.findViewById(R.id.container);
linearLayoutG.setBackgroundColor(Color.parseColor("#26ffffff"));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 0, 10, 30);
linearLayoutG.setLayoutParams(layoutParams);
//here i create spinner
View linearLayout = convertView.findViewById(R.id.spinnerL);
final Spinner spinner = new Spinner(context);
spinner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
//code
((LinearLayout) linearLayout).addView(spinner);
(sorry about links, i don't have 15 reputation)
Result on android 4.0.1
Result on android 5+
If i use static size, like this:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 120);
Here's what happens
Result on android 4.0.1
Result on android 5+
Well as i can see in your code :
spinner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
you are setting the height to MATCH_PARENT. I have no idea why it is working on 4.0.1 but change your code to this:
spinner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Related
I have been looking these past two days on questions regarding setting the weight of a layout or a group of layouts programmatically.
all the answers i found are almost the same so that means i know what code no to use but i do not seem to understand how to assign the float attribute. in the following code.
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
);
YOUR_VIEW.setLayoutParams(param);
would someone please give me an example of how to assign the weights of two TextView with a weight sum of 3???
It depends on your view if you want to split the view horizontally between them you can use something like this.
TextView secondTV = findViewById(R.id.secondTextView);
TextView firstTV = findViewById(R.id.firstTextView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 3);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
firstTV.setLayoutParams(layoutParams);
secondTV.setLayoutParams(layoutParams1);
And your layout looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/firstTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello" />
<TextView
android:id="#+id/secondTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/icon"
android:text="Hello" />
</LinearLayout>
but if you want to split the view vertically you can use something like this.
TextView secondTV = findViewById(R.id.secondTextView);
TextView firstTV = findViewById(R.id.firstTextView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 3);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
firstTV.setLayoutParams(layoutParams);
secondTV.setLayoutParams(layoutParams1);
And your layout looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/firstTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello" />
<TextView
android:id="#+id/secondTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/icon"
android:text="Hello" />
</LinearLayout>
This is my xml created file for a layout and it displays as I want it to.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#android:drawable/dialog_holo_light_frame">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="#+id/imageSectionCustomRowL"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:id="#+id/titleCustomRowL"
android:paddingRight="10dp"
android:textSize="30sp"
android:textColor="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#959595"
android:paddingBottom="10dp"
android:paddingTop="5dp"
android:id="#+id/subTitleCustomRowL"
android:paddingLeft="20dp"
android:paddingRight="10dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And this is more or less the same thing but created dynamically via code
LinearLayout parentLL = (LinearLayout)findViewById(R.id.newsMainPopulationLL);
LinearLayout parentRowLL = new LinearLayout(news.this);
parentRowLL.setOrientation(LinearLayout.VERTICAL);
Drawable bg = news.this.getResources().getDrawable(android.R.drawable.dialog_holo_light_frame);
//Drawable bbg = getDrawable(android.R.drawable.dialog_holo_light_frame);
parentRowLL.setBackgroundResource(android.R.drawable.dialog_holo_light_frame);
parentRowLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertDpToPx(120)));
LinearLayout parentRowChildHLL = new LinearLayout(news.this);
parentRowChildHLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
parentRowChildHLL.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout containerForPic = new LinearLayout(news.this);
containerForPic.setLayoutParams(new LinearLayout.LayoutParams(convertDpToPx(75), convertDpToPx(75)));
containerForPic.setOrientation(LinearLayout.VERTICAL);
ImageView picInList = new ImageView(news.this);
picInList.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
LinearLayout contentVerticalLL = new LinearLayout(news.this);
contentVerticalLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
contentVerticalLL.setOrientation(LinearLayout.VERTICAL);
TextView headingTV = new TextView(news.this);
headingTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
headingTV.setPadding(convertDpToPx(10), convertDpToPx(10), convertDpToPx(10), 0);
headingTV.setTextColor(Color.parseColor("#000000"));
headingTV.setTextSize(30);
TextView contentTV = new TextView(news.this);
contentTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contentTV.setPadding(convertDpToPx(10), convertDpToPx(5), convertDpToPx(10), convertDpToPx(10));
contentTV.setTextColor(Color.parseColor("#959595"));
contentVerticalLL.addView(headingTV);
contentVerticalLL.addView(contentTV);
parentRowChildHLL.addView(contentVerticalLL);
containerForPic.addView(picInList);
parentRowChildHLL.addView(containerForPic);
parentRowLL.addView(parentRowChildHLL);
parentLL.addView(parentRowLL);
Here convertDptoPx is a int() that works properly. However for some reason the Linear layout parentRowChildLL even though set to LinearLayout.HORIZONTAL for orientation for some reason displays vertically (According to the layout boundaries I see using the developer options setting)
Is there something basic I'm missing out yet? When I see the boundaries I have 3 rows. And also I tried encasing my image view in the layout because otherwise it is not even visible.
Thank You.
I want to set the buttons one after another vertically. I was trying something like below, but it doesn't work. Need help please.
RelativeLayout body=(RelativeLayout) findViewById(R.id.body);
RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btn;
List<Button> allEds = new ArrayList<Button>();
for(int i=0;i<totalSID;i++){
btn = new Button(FaqList.this);
btn.setId(i);
allEds.add(btn);
if(i==0){}
else
buttonParams.addRule(RelativeLayout.BELOW,allEds.get(i-1).getId());
body.addView(btn,buttonParams);
}
Adding the xml file. For reference you can go through it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical"
>
<HorizontalScrollView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:fillViewport="true"
>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="20dp"
>
<LinearLayout
android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:animateLayoutChanges="true">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout
I am not sure where is the problem actually
If you want to add buttons vertically then just use LinearLayout instead of RelativeLayout in your body.
int iNumberOfButtons = productTypeList.size();
Button[] dynamicButtons = new Button[iNumberOfButtons];
LinearLayout.LayoutParams paramsButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < iNumberOfButtons; i++) {
ProductType productType = productTypeList.get(i);
dynamicButtons[i] = new Button(getActivity());
dynamicButtons[i].setText(productType.getTitleString());
dynamicButtons[i].setId(i);
dynamicButtons[i].setTextSize(15.0f);
dynamicButtons[i].setOnClickListener(this);
dynamicButtons[i].setLayoutParams(paramsButton);
dynamicButtonsLinearLayout.addView(dynamicButtons[i]); // dynamicButtonsLinearLayout is the container of the buttons
}
Use linear layout instead of Relative layout. If you need to add multiple button dynamically use RecyclerView based on Item position you can do whatever you need
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button bt=new Button(this);
bt.setLayoutParams(lparams);
this.m_vwJokeLayout.addView(bt);
you used relative layout . with that bottons cover eachother . use linearlayout as parent container . but if you want use relative layout you should store previous buttons id and set new buttons below property like this :
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow);
At the moment everything is done through the java code. I'm creating a relative layout then adding my GL surface view and some text views to it. the problem is I cant align one text view to the top left and the other to the top right. Whats wrong with my code. Also is there a more efficient way of doing this such as doing this through XML instead. Cause I've tried this and it didn't work.
r1 = new RelativeLayout(this);
view = new MyGLSurfaceView(this);
view.setKeepScreenOn(true);//keeps activity from going to sleep
r1.addView(view);
TextView text1 = new TextView(this);
TextView text2 = new TextView(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 50);
RelativeLayout.LayoutParams rp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 200);
lp.addRule(RelativeLayout.ALIGN_RIGHT);
text1.setLayoutParams(lp);
text1.setText("3 Star");
text1.setBackgroundColor(0x4060ff70);
rp.addRule(RelativeLayout.ALIGN_LEFT);
text2.setLayoutParams(rp);
text2.setText("4 Star");
text1.setTextSize(30);
text2.setTextSize(30);
//text2.setBackgroundColor(0x4060ff70);
r1.addView(text1);
r1.addView(text2);
setContentView(r1);
This can be done in XML like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left" />
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right" />
</RelativeLayout>
I have an Android app with this portion of main activity:
<LinearLayout
android:id="#+id/vistaAnni"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView02"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:text="1998" />
<CheckBox
android:id="#+id/CheckBox02"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1" />
<CheckBox
android:id="#+id/CheckBox07"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1" />
On the main activity class, I need another linear layout with other checks box.
LinearLayout myView = (LinearLayout) findViewById(R.id.vistaAnni);
Log.v("blah", "Crea tabella dinamica");
for(int i = 0; i < anni.size();i++){
Log.v("blah", "Creo colonna: "+anni.get(i));
LinearLayout newLine = new LinearLayout(getApplicationContext());
newLine.setOrientation(1);
newLine.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
TextView testo = new TextView(getApplicationContext());
testo.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, .1f));
testo.setText(anni.get(i));
newLine.addView(testo);
CheckBox check1c = new CheckBox(getApplicationContext());
check1c.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, .1f));
newLine.addView(check1c);
CheckBox check2c = new CheckBox(getApplicationContext());
check2c.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, .1f));
newLine.addView(check2c);
myView.addView(newLine);
The system creates the box and the text fine, but with different colors and hardly visibility.
But I haven't customized it over activity.xml it or over activity.java. Why this change?
This is a sreen of app:
I have change all:
CheckBox check1c = new CheckBox(getApplicationContext());
with:
CheckBox check1c = new CheckBox(this);
as suggested by Luksprog and works.