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);
Related
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
I want to make candidateView inside button but
you see log cat:
please share code
My Code
SoftKeyboard.java
#Override
public View onCreateCandidatesView() {
LayoutInflater li = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View wordBar = li.inflate(R.layout.wordbar, null);
LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.words);
Button btn = (Button) wordBar.findViewById(R.id.button1);
btn.setOnClickListener(this);
mCandidateView = new CandidateView(this);
mCandidateView.setService(this);
setCandidatesViewShown(true);
mCandidateView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(mCandidateView);
return wordBar;
}
I want to make like this:
Layout wordBar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/words"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Thanks for advance
See this line
LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.words);
You are casting a TextView into LinearLayout
<TextView
android:id="#+id/words"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
R.id.words is a TextView
Solution
Add an id to your LinearLayout
Example :android:id="#+id/wordsLayout"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/wordsLayout"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/words"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
And use that id
LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.wordsLayout);
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
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!
I need help with my android app. I need inflate a layout within another layout and I dont know how I do. My xml code is this:
item.xml - I need inflate multiple xml (depending on a variable number)
<RelativeLayout
android:id="#+id/cartel_N1"
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="#color/tabhost_background_pressed"
android:layout_marginRight="22dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_N1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:src="#drawable/mcdonalds_icon" />
<TextView
android:id="#+id/title_N1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="McDonals del CC Alcampo"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textSize="15sp" />
<TextView
android:id="#+id/categoria_N1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="CATEGORIA"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
<RelativeLayout
android:id="#+id/stars_and_distance_N1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/stars_N1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stars_25"
android:layout_marginLeft="15dp"
android:layout_marginTop="7dp" />
<TextView
android:id="#+id/distance_N1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200m"
android:textSize="12sp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:gravity="center_vertical"
android:layout_marginTop="3dp" />
<ImageView
android:id="#+id/icon_pos_N1"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="#drawable/marker_distance"
android:layout_toLeftOf="#id/distance_N1"
android:layout_marginTop="7dp" />
</RelativeLayout><LinearLayout
android:id="#+id/cartel_N1"
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="#color/tabhost_background_pressed"
android:layout_marginRight="22dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_N1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:src="#drawable/mcdonalds_icon" />
<TextView
android:id="#+id/title_N1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="McDonals del CC Alcampo"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textSize="15sp" />
<TextView
android:id="#+id/categoria_N1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="CATEGORIA"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
<RelativeLayout
android:id="#+id/stars_and_distance_N1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/stars_N1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stars_25"
android:layout_marginLeft="15dp"
android:layout_marginTop="7dp" />
<TextView
android:id="#+id/distance_N1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200m"
android:textSize="12sp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:gravity="center_vertical"
android:layout_marginTop="3dp" />
<ImageView
android:id="#+id/icon_pos_N1"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="#drawable/marker_distance"
android:layout_toLeftOf="#id/distance_N1"
android:layout_marginTop="7dp" />
</RelativeLayout>
This is my main xml:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/container_destacado"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Inflate multiple xml file here -->
</LinearLayout>
</ScrollView>
You could use something like
LayoutInflater inflater = LayoutInflater.from(context);
//to get the MainLayout
View view = inflater.inflate(container_destacado, null);
...
//Avoid pass null in the root it ignores spaces in the child layout
View inflatedLayout= inflater.inflate(R.layout.yourLayout, (ViewGroup) view, false);
containerDestacado.addView(inflatedLayout);
You can implement this like below:
LayoutInflater linf;
LinearLayout rr;
linf = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
linf = LayoutInflater.from(activity.this);
rr = (LinearLayout) findViewById(R.id.container_destacado);
for (int i = 1; i < NoOfTimes; i++) {
final View v = linf.inflate(R.layout.item, null);
rr.addView(v);
}
ll = (LinearLayout) findViewById(R.id.container_destacado); // ll is the layout where your inflated layout will be added
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int pos = 0;
while (pos < noOfTimes)
{
View myView = linflater.inflate(R.layout.item, null); //here item is the the layout you want to inflate
myView.setId(pos);
/*
You can change TextView text and ImageView images here e.g.
TextView tv = (TextView)myView.findViewById(R.id.title_N1);
tv.setText(pos);
*/
pos++;
ll.addView(myView);
}
In some point you should gain access to your inflater inside your activity, you can call it with this code:
LayoutInflater li = LayoutInflater.from(context);
Where context is this or this.getActivity() if it is a Fragment. Then inflate your layour with:
View layout = li.inflate(R.layout.your_layout, null, false);
Then use addView(layout) to your container_destacado:
View containerDestacado = li.inflate(R.layout.container_destacado, null, false);
containerDestacado.addView(layout);
Hope it helps.
Kotlin code to do so:
val layoutToInflate =
this.layoutInflater.inflate(R.layout.ly_custom_layout,
null)
container_destacado.addView(layoutToInflate )
You have the LinearLayout on which you want to inflate other childs:
LinearLayout container = (LinearLayout)parentView.findViewById(R.id.container_destacado);
Once you loaded the item.xml with an inflater, you can just use
container.addView(itemView);
in Kotlin with editable textview elements on item.xml
val item = LayoutInflater.from(this).inflate(R.layout.item,null)
val distance_N1_holder = item.findViewById<TextView>(R.id.distance_N1)
distance_N1_holder.text = //dynamically generated value
main.addView(item)
Below is a working code from one of my projects:
// The parent container
mNotificationOverlay = (LinearLayout) findViewById(R.id.container_destacado);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (Message message : m) {
// Inflate the child layout on the fly
final View notificationContainer = inflater.inflate(R.layout.notification_overlay_linear_layout, null);
notificationContainer.setTag(message.getNotificationId());
// Access children of child container
TextView notificationOverlayTitle = (TextView) notificationContainer.findViewById(R.id.notification_title_overlay);
TextView notificationOverlayBody = (TextView) notificationContainer.findViewById(R.id.notification_body_overlay);
ImageButton notificationOverlayCancelButton = (ImageButton) notificationContainer.findViewById(R.id.notification_cancel_overlay);
// Perform desired operations
notificationOverlayCancelButton.setTag(message.getNotificationId());
notificationOverlayTitle.setText(message.getTitle());
notificationOverlayBody.setText(message.getNotificationBody());
mNotificationOverlay.setVisibility(View.VISIBLE);
// Attach any listeners
attachListenersToCancelView(notificationOverlayCancelButton);
// Add view to parent container
mNotificationOverlay.addView(notificationContainer);
}
Hope that helps!
If someone couldn't get the addView() it's because the view is not a layout.