Inflate layout programmatically within another layout - android

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.

Related

how to set the specific row item option(visible, invisible) in listview in android

frist my english skill weak.
description>
this is list view.
ㅡㅡㅡㅡㅡㅡㅡㅡ
ㅣㅡㅡㅡㅡㅡㅣㅢ <-- this is button , i init set invisible
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡ ////// <-- i want make visible button
ㅣㅡㅡㅡㅡㅡ ////// <-- specific position
I make the custom ListView
ListView row contains texts, Button.
The Button is set invisible option in xml file.
then, I want set the visible specific row button.
I tried that and failed
after make ArrayList for ListView, marking matching position like this
for(i=0; i<arraylist.size(); i++){
int t41=Integer.parseInt(arraylist.get(i).getm());
if(month == t41){
confirm_replay[i]=true;
temp55=i;
}
}
I can set the textValue. through adapter.getItem(int position).
but i don't know, how to control specific button.
also try add button into adapter. failed..
also search question in google but my eng skill bad. failed
add my code.
main.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">
<TextView
android:paddingTop="10dp"
android:text="match day(weekend)"
android:textSize="28dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:background="#2d000000"
android:layout_width="match_parent"
android:layout_height="3dp">
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
list.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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/m"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:paddingLeft="10dp"
android:id="#+id/d"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/yoil"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/time"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/vsTeam"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/league"
android:paddingLeft="10dp"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/공갈"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_youtube"
android:text="다시보기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
</LinearLayout>
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/m"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:paddingLeft="10dp"
android:id="#+id/d"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/yoil"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/time"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/vsTeam"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/league"
android:paddingLeft="10dp"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/공갈"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_youtube"
android:text="다시보기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
</LinearLayout>
</b>
adapter
class MlistViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<MatchInfomation> matchinformationlist = null;
private ArrayList<MatchInfomation> arraylist;
public MlistViewAdapter(Context context,
List<MatchInfomation> matchinformationlist) {
mContext = context;
this.matchinformationlist = matchinformationlist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<MatchInfomation>();
this.arraylist.addAll(matchinformationlist);
}
public class ViewHolder {
TextView m;
TextView d;
TextView yoil;
TextView vsTeam;
TextView time;
TextView league;
}
#Override
public int getCount() {
return matchinformationlist.size();
}
#Override
public MatchInfomation getItem(int position) {
return matchinformationlist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.activity_match_list, null);
button_youtube.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setPackage("com.google.android.youtube");
intent.putExtra("query", "Android");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
});
// Locate the TextViews in listview_item.xml
holder.m = (TextView) view.findViewById(R.id.m);
holder.d = (TextView) view.findViewById(R.id.d);
holder.yoil = (TextView) view.findViewById(R.id.yoil);
holder.vsTeam = (TextView) view.findViewById(R.id.vsTeam);
holder.time = (TextView) view.findViewById(R.id.time);
holder.league = (TextView) view.findViewById(R.id.league);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.m.setText(matchinformationlist.get(position).getm());
holder.d.setText(matchinformationlist.get(position).getd());
holder.yoil.setText(matchinformationlist.get(position).getyoil());
holder.vsTeam.setText(matchinformationlist.get(position).getvsTeam());
holder.time.setText(matchinformationlist.get(position).gettime());
holder.league.setText(matchinformationlist.get(position).getleague());
return view;
}
}
If you want to adjust a specific row, you can use the position parameter in your getView() method in adapter. For instance;
if(position==55){
holder.m.setVisibility(View.GONE);
}
I'm not sure i understand your question.
If i'm right you juste want your button to be invisble for specific row. To do so add the specific line at the end of your getView method
yourButton.setVisibility(View.INVISIBLE)
If you want to hide the entire row, the best way will probably be to change your adapter to diplay only row with content.
before setting the values to the view check the condition whatever you want like:-
if condition is true then set your view visible
else set your view invisible
if(true){
button.setVisibility(View.VISIBLE);
}else{
button.setVisibility(View.GONE);
}

Custom ListView Item barely visible

I'm having a problem that all my ListViews Items are showing like this:
As you guys can see Hello World! is a TextView and is showing perfectly.
But the ListView itens are showing like they are disabled.
This is my Activity's onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open_answers);
ctx = getApplicationContext();
Intent intent = getIntent();
usersurvey_id = intent.getIntExtra("usersurvey_id", -1);
list = (ListView) findViewById(R.id.listview_answers);
getAnswers();
AnswersArrayAdapter adapter = new AnswersArrayAdapter(ctx, answersList);
list.setAdapter(adapter);
list.setClickable(false);
list.setEnabled(true);
}
This is my Custom Adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent){
Answers answerItem = answersList.get(position);
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listitem_answers, null);
holder = new ViewHolder();
holder.question = (TextView) convertView.findViewById(R.id.tv_texto_pergunta);
holder.answer = (TextView) convertView.findViewById(R.id.tv_texto_resposta);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.question.setText(getQuestionById(answerItem.getAnswer_question_id()));
holder.answer.setText(answerItem.getAnswer_answer());
return convertView;
}
Activity layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.seven.questionario.OpenAnswers">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/listview_answers"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
And ListView Item Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_pergunta"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Pergunta:"
android:background="#D0D0D0"/>
<TextView
android:id="#+id/tv_texto_pergunta"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:layout_below="#id/tv_pergunta"
android:text="Texto da pergunta"
android:background="#D0D0D0"/>
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:background="#drawable/dotted"
/>
<TextView
android:id="#+id/tv_resposta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:text="Resposta:"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_texto_resposta"
android:layout_width="fill_parent"
android:layout_height="80sp"
android:layout_below="#id/tv_resposta"
android:text="Texto da resposta" />
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:layout_below="#id/tv_texto_resposta"
/>
I now this is a bit old but i just stumbled across this post.
Could it be that you're using the wrong context? I think you should pass the current context of your Activity, like so:
ctx = getContext();
or like this:
AnswersArrayAdapter adapter = new AnswersArrayAdapter(this, answersList);
try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D0D0D0">
<TextView
android:id="#+id/tv_pergunta"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Pergunta:"/>
<TextView
android:id="#+id/tv_texto_pergunta"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:layout_below="#id/tv_pergunta"
android:text="Texto da pergunta"/>
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:background="#drawable/dotted"
/>
<TextView
android:id="#+id/tv_resposta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:text="Resposta:"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_texto_resposta"
android:layout_width="fill_parent"
android:layout_height="80sp"
android:layout_below="#id/tv_resposta"
android:text="Texto da resposta" />
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:layout_below="#id/tv_texto_resposta"
/>
Show your theme(application/activity) selected in AndroidManifest.xml. I guess it's the problem.
I followed #Aun tip and solved my problem.
I just had to set manually all my app's colors.
try this line
android:textColor="#000000" in your TextView

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);

Adding Views programatically through array adapter

I am displaying a listview using an arrayadapter. for each listitem i inflate the same xml file.i am able to display the listview fine.but now i have another requirement. i want to add some more view programatically to the same xml file that i am inflating for each list item. but when i try to do that i am not getting any error. but somehow the view i added programatically is not showing up...can anyone please help me. below is the code for the xml layout and for the arrayadapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/listitemcolor"
android:padding="10dip">
<LinearLayout
android:layout_weight="1.0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:textColor="#android:color/black"
android:id="#+id/locationitem_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textSize="16dp"
android:textStyle="bold" />
<LinearLayout
android:layout_marginTop="5dip"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:drawableLeft="#drawable/like"
android:textColor="#008000"
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Likes"
android:textSize="12dp"
android:textStyle="bold" />
<TextView
android:textColor="#008000"
android:layout_marginLeft="5dip"
android:id="#+id/number_of_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:drawableLeft="#drawable/dislike"
android:textColor="#FF0000"
android:layout_marginLeft="10dip"
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dislikes"
android:textSize="12dp"
android:textStyle="bold" />
<TextView
android:textColor="#FF0000"
android:layout_marginLeft="5dip"
android:id="#+id/number_of_dislikes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
This is the code for arrayadapter:
public class LocationItemAdapter extends ArrayAdapter<LocationItem> {
private Context context;
private ArrayList<LocationItem> items;
public LocationItemAdapter(Context context,
List<LocationItem> objects) {
super(context, R.layout.locationitem_listview_element, objects);
this.context = context;
items = (ArrayList<LocationItem>) objects;
}
/* (non-Javadoc)
* #see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowview = null;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowview = inflater.inflate(R.layout.locationitem_listview_element, null);
}
else
{
rowview = convertView;
}
((TextView)rowview.findViewById(R.id.locationitem_name)).setText(items.get(position).getName());
((TextView)rowview.findViewById(R.id.number_of_likes)).setText(String.valueOf(items.get(position).getLikes()));
((TextView)rowview.findViewById(R.id.number_of_dislikes)).setText(String.valueOf(items.get(position).getDislikes()));
LayoutInflater inflater1 = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView tv = new TextView(context);
//TextView tv = (TextView)inflater1.inflate(R.layout.sample, null);
tv.setText("My name is blah");
tv.setTextColor(R.color.black);
tv.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
//tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
LinearLayout ll = (LinearLayout)rowview.findViewById(R.id.placeholder);
//((LinearLayout)rowview.findViewById(R.id.placeholder)).addView(tv);
ll.addView(tv);
rowview.setTag(items.get(position));
return rowview;
}
}
That looks correct to me. Perhaps the embed is successful but ll takes up no space on the screen? Can you verify that placeholder takes up some amount of visible space on the screen by setting it's background color to hot pink or something?

Error in a dynamic text view based on the xml file

i am having a dynamic text view based on the xml file.....Please help me for my code......
my codes
MessageViewPage.java
package com.dpn.pack;
public class MessageViewPage extends Activity {
TextView tv1=new TextView(this);
ScrollView sv;
String nickname,body;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message_view_page);
Bundle b = getIntent().getExtras();
nickname= b.getString("nick");
body=b.getString("body");
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.message_view_page, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.textViewSender);
textView.setText("Sender : "+nickname);
// insert into main view
View insertPoint = findViewById(R.id.textViewSender);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
}
my xml file as follows
message_view_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:background="#drawable/view">
<TextView
android:id="#+id/textViewSender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="35dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<TextView
android:id="#+id/textViewBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textViewSender"
android:layout_below="#+id/textViewSender"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:textSize="15dp"
android:textColor="#F69DD1" />
<Button
android:id="#+id/buttonReply"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="45dp"
android:layout_marginBottom="50dp"
android:text="Reply" />
<Button
android:id="#+id/buttonListen"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="45dp"
android:layout_marginBottom="50dp"
android:text="LIsten" />
</RelativeLayout>
i am having an error in
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Please any one tell me the solution.....
Your insertPoint is null. Because you don't set the layout before calling findViewById. So it will not find anything.
You have to call
setContentView(R.layout.message_view_page);
before.
you use a line RelativeLayout item = (RelativeLayout) findViewById(R.id.textViewSender);
in your code but hoe can you get textViewSenderid with out adding your xml file message_view_page.xml
you should add
setContentView(R.layout.message_view_page);
after
super.onCreate(savedInstanceState); this line.

Categories

Resources