How to programmatically (dynamically) center a LinearLayout inside a RelativeLayout? - android

I want to dynamically center a LinearLayout view inside its RelativeLayout parent and so I want to use android.R.attr.layout_centerInParent.
How do I apply it?
I tried the following but the child view does not appear at all.
child view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text2"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
parent view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<FrameLayout
android:id="#+id/my_preview"
android:layout_width="match_parent"
android:layout_height="407dp" >
</FrameLayout>
<include
android:id="#+id/my_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="408dp"
layout="#layout/my_bar" />
</RelativeLayout>
in my activity
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout rootView = (RelativeLayout) inflater.inflate(id, null);
LinearLayout textBox = (LinearLayout) inflater.inflate(R.layout.child_view, null);
TextView line2 = (TextView) textBox.findViewById(R.id.text2);
line2.setText("hahahaha");
TextView line1 = (TextView) textBox.findViewById(R.id.text1);
line1.setText("rrrrrrrrrr");
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(android.R.attr.layout_centerInParent);
textBox.setLayoutParams(rlp);
rootView.addView(textBox);
setContentView(rootView);
I changed the child view to a RelativeLayout but still cannot see the child view.

For some reason, android.R.attr.layout_centerInParent fails.
I used
rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
and it worked!

Related

how to display a layout dynamically with margin

I have two layouts, i inflate one layout into another and want to display this inflated layout including textview as many times as condition is true . I set the textview value as value of i and also set the layout top margin. but it displaying value of i in one row only and rest are blank,also its setMargin() also not working.Below is what i have done yet.
public void displayRows(){
int n= 6;
for(int i=0;i< n;i++){
LinearLayout container = (LinearLayout)findViewById(R.id.layoutContainer);
View hiddenInfo = getLayoutInflater().inflate(R.layout.dynamic, container, false);
container.addView(hiddenInfo);
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.dynamic);
TextView textView = (TextView) findViewById(R.id.text);
textView.setText("Text"+i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 30, 0, 0);
hiddenLayout.setLayoutParams(layoutParams);
hiddenLayout.addView(textView);
}
container.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/layoutContainer"
android:animateLayoutChanges="true"
android:layout_gravity="center|top"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/li1"
android:layout_gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#color/colorAccent"
android:onClick="displayRows"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_gravity="center"
android:text="Rows"/>
</LinearLayout>
</LinearLayout>
dynamic.xml
<?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">
<LinearLayout
android:id="#+id/dynamic"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:text=""
android:textColor="#color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I also want to know how to remove all the generated dynamic rows on click remove button.
Replace
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.dynamic);
TextView textView = (TextView) findViewById(R.id.text);
with
LinearLayout hiddenLayout = (LinearLayout) hiddenInfo.findViewById(R.id.dynamic);
TextView textView = (TextView) hiddenInfo.findViewById(R.id.text);
And remove this line
hiddenLayout.addView(textView);

Inflating View into LinearLayout and editing cmponents in this view

I am trying to add multiple LinearLayouts into one declared in xml. Everyone has 3 textView which will be edited in code. My problem is, when i am inflating xml layout to View object in code, all margins are ignored. My second question:
How can i dynamically set ids to textViews and then edit text in it?
LinearLayout xml which is inflating:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/pointsAwaiting"
android:layout_marginTop="40dp"
android:background="#drawable/background_blue"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:textColor="#FFFFFF"
android:textSize="15dp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
</LinearLayout>
He is inflating into this piece of code:
<
ScrollView 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"
android:background="#drawable/background"
tools:context="pl.com.qiteq.zielonomocni_rework.HistoryActivity">
<LinearLayout
android:id="#+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:id="#+id/historyView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</ScrollView>
And finnaly java code: (loop counter is for example)
LinearLayout mainView = (LinearLayout) findViewById(R.id.historyView);
for (int i=0; i<=2; i++){
View layout = View.inflate(this, R.layout.history_bar, null);
mainView.addView(layout);
}
The reason all your margins are being ignored is that you are passing null into your inflater here:
View layout = View.inflate(this, R.layout.history_bar, null);
When you do this all the LayoutParams of your view are thrown away. You should replace it with:
View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
To set text in the TextViews you don't need to set a different id for each one, just give each one an id. Then in your loop you can do something like:
List<TextView> textViews = new ArrayList<>();
LayoutInflater inflater = LayoutInflater.from(this);
for (int i=0; i<=2; i++){
View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
mainView.addView(layout);
TextView textView = (TextView) layout.findViewById(R.id.text1);
textViews.add(textView);
}
You would then have a reference to each of your TextViews in the List

Want to align linearlayout inside another, but setGravity() doesn`t work

I am trying to align a linear layout inside another linear layout programmatically, but setGravity(Gravity.RIGHT) doesn`t work.
I know the differences between gravity and layout_gravity.
Here's the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/initial_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="#+id/conversationwrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bubble_yellow"
>
<TextView
android:id="#+id/messagecontent_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:paddingLeft="10dip"
android:text="Message_content"
android:textSize="15dp" />
<TextView
android:id="#+id/timestamp_tv"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="HH:MM"
android:ems="2"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
And the code of getView() in my Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_conversation, parent, false);
}
wrapper = (LinearLayout) row.findViewById(R.id.conversationwrapper);
initial_wrapper = (LinearLayout) row.findViewById(R.id.initial_wrapper);
time = (TextView) row.findViewById(R.id.timestamp_tv);
Message coment = getItem(position);
content = (TextView)row.findViewById(R.id.messagecontent_tv);
content.setText(coment.content);
time.setText(getCurrentTimeStamp());
wrapper.setBackgroundResource(!coment.mine ? R.drawable.bubble_yellow : R.drawable.bubble_green);
initial_wrapper.setGravity(!coment.mine ? Gravity.LEFT : Gravity.RIGHT);
return row;
}
But it always happen to be on the left, even when the image is green.
The strange thing is that if I type
android:gravity="right"
inside initial_wrapper, It works in the preview but not in the device.
I'm using a Moto G and a Nexus 5, but neither of them work
The problem can be here : android:layout_width="fill_parent", how you supose to set android:gravity="right" here, if your layout is filling whole parent? You should try to use
<LinearLayout
android:id="#+id/initial_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
instead of :
<LinearLayout
android:id="#+id/initial_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
do this
<LinearLayout
android:id="#+id/initial_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity = "left"
>
in your getView() do this
LinearLayout.LayoutParams lp = initial_wrapper.getLayoutParams();//will need to casting
lp.gravity= Gravity.RIGHT; // this is the important part
initial_wrapper.setLayoutParams(lp);
hope it helps

Android LinearLayout not displaying correctly if number of items is lesser then screen height

I have a fragment which fetch items from rest api and display the item title in LinearLayout within a scrollview.
Below are my views and codes.
fragment_cards_view.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:fillViewport="true">
<LinearLayout
android:id="#+id/linear_card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/wallet_holo_blue_light"/>
</ScrollView>
list_item.xml
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:layout_marginBottom="10dp">
<TextView
android:id="#+id/list_titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
style="#style/item_header"/>
</RelativeLayout>
</RelativeLayout>
TitleFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "In onCreateView");
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_cards_view, container, false);
mLinearLayout = (LinearLayout)v.findViewById(R.id.linear_card_view);
return v;
}
private void createUpcomingView() {
int counter = 0;
for (User user : mUsers) {
LayoutInflater inflater = null;
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = inflater.inflate(R.layout.list_item, null);
mLinearView.setId(user.getId());
TextView titleTextView = (TextView)mLinearView.findViewById(R.id.list_titleTextView);
titleTextView.setText("title " + counter);
mLinearLayout.addView(mLinearView);
counter++;
}
}
The problem I faced is that if the number of items is lesser than the screen height, the display becomes incorrect.
This screenshot has 4 items, but it is only showing the first item and the margins are not aligned correctly.
This screenshot has many items and the margins are aligned correctly.
This screenshot is the first solution:
Remove following layout from list_item.xml then it will work properly.
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:layout_marginBottom="10dp">
</RelativeLayout>
Try writing
inflater.inflate(R.layout.list_item, mLinearLayout, false);
instead of
inflater.inflate(R.layout.list_item, null);
Also, use wrap_content heights in list_item.xml
I have found the solution based on the answers from #clemp6r and #Jain Nidhi.
fragment_cards_view.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:fillViewport="true">
<LinearLayout
android:id="#+id/linear_card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/wallet_holo_blue_light"/>
</ScrollView>
list_item.xml, add left and right margin, removed nested RelativeLayout, set all layout_height to wrap_content
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:id="#+id/list_titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
style="#style/item_header"/>
</RelativeLayout>
TitleFragment.java, modified the 2nd and 3rd argument of inflate.
private void createUpcomingView(..) {
View mLinearView = inflater.inflate(R.layout.list_item, mLinearLayout, false);
}

adding a dynamic linearlayout to the current view android

I am trying to add a linearlayout to a scrolview
this is my code
the code compiles, but it doesn't show me the new layout
this is the original layout (that i want to add to it)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_margin="15dp"
android:layout_marginTop="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="#+id/ViewHistoryImageLayout">
<ImageView
android:id="#+id/HistoryImage"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.76"
android:gravity="center"
android:padding="10dp"
android:src="#drawable/upload"
android:contentDescription="#string/HistoryImage"/>
<TextView
android:id="#+id/TranslatedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.12"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/translateImageButton" />
</LinearLayout>
and this is the layout that i want to add several times:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/TranslationMenuLayout" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numStars="5" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
and the java code for adding the new layout is:
setContentView(R.layout.activity_view_history_image);
ScrollView sv = new ScrollView(this);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate(R.layout.translation_menu, null);
sv.addView(ll);
the code compiles fine, and the app is running but nothing happens
is there a problem with one of the .xml files?
tnx
you should use an inflater,
change:
LinearLayout ll = new LinearLayout(R.layout.translation_menu);
with
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate(R.layout.translation_menu, null);
sv.addView(ll);
The LayoutInflater creates a View object from the corresponding XML file.
LayoutInflater li = LayoutInflater.from(context);
LinearLayout ll = (LinearLayout) li.inflate(R.layout.translation_menu, this)
This is the Correct way.
Use a LayoutInflator.
LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
In other words, it takes as input an XML file and builds the View objects from it.
ScrollView sv = new ScrollView(this);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
LinearLayout ll = (LinearLayout) li.inflate(translation_menu, this);
sv.addView(ll);

Categories

Resources