Adding a ViewGroup to a ViewGroup - android

I would like to be able to programmatically add a viewgroup to another viewgroup. Both have been defined in xml as follows, along with my onCreate method:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first text" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second text" />
secondlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/secondll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first text" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second text" />
onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context context = getBaseContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.main, null);
LinearLayout tv = (LinearLayout) inflater.inflate(R.layout.secondlayout, null);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(tv);
setContentView(ll);
}

You are trying to find a view before you set the content view when you do findViewById(R.layout.secondlayout). Also, secondlayout isn't the id of a view, it's the name and id of the layout file.
Try doing
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.main, null);
LinearLayout tv = (LinearLayout) inflater.inflate(R.layout.secondlayout, null);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(tv);
setContentView(ll);

Related

Android: How to make button on candidate view in softkeyboard?

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

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

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!

setText to TextView when using the inflater

In my android app i have been using the RelativeLayout and inflater. so i need to set the text in textView, which i have used with Inflater, but i cannot set the text by code.
XML file activity_title
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent"
android:background="#000000" >
<TextView
android:id="#+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Text" />
</RelativeLayout>
2 xml file
<RelativeLayout
android:id="#+id/rel_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/linearLayout1"
android:layout_toRightOf="#+id/linearLayout1"
>
</RelativeLayout>
so this is the code, which using the inflater
relativeLayout = (RelativeLayout) findViewById(R.id.rel_container);
((ViewGroup) relativeLayout).removeAllViews();
for (int i = 0; i < titleCalendar.length; i++) {
getApplicationContext();
vi= (LayoutInflater)
getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.activity_title, null);
textViewC = (TextView) v.findViewById(R.id.text_title);
textViewC.setText("sdfsfasfasfasfasf");
textViewC.setTextColor(Color.parseColor("#ffffff"));
final RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, addcnvrtTime);
textViewC.setLayoutParams(params);
((ViewGroup) relativeLayout).addView(v, 0, params);
}
I cannot see the text, which i set on that textview
This is to get Inflater elements:
LayoutInflater inflater = getLayoutInflater();
View customView = inflater.inflate(R.layout.YOUR_LAYOUT_INFLATER, null);
TextView text = (TextView)customView.findViewById(R.id.YOUR_TEXT_VIEW);
text.setText("BlaBlaBlaBlaBla");
Why not put TextView direct inside the Layout and simply do
TextView text = (TextView)findViewById(R.id.YOUR_TEXT_VIEW);
text.setText("BlaBlaBlaBlaBla");
?
try remove params.addRule(RelativeLayout.BELOW, addcnvrtTime)?

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

How to make a view appear below

Hey I want to make a view appear below the last that has been created.
It is showing like this:
I want to make the next view show below the last one, so for each new view added, it shows below. Understand?
Here is my code. The xml file and the java file.
listitem.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="#+id/linearId"
android:padding="6dip"
>
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip" android:layout_weight="1"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/txt1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="#+id/txt2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
</LinearLayout>
RatedCalls.java
public class RatedCalls extends Activity {
private static final String LOG_TAG = "RatedCalls";
private TableLayout table;
private CallDataHelper cdh;
private TableRow row;
private TableRow row2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listitem);
Log.i(LOG_TAG, "calling from onCreate()");
cdh = new CallDataHelper(this);
startService(new Intent(this, RatedCallsService.class));
Log.i(LOG_TAG, "Service called.");
Log.i(LOG_TAG, "before call fillList");
List<String> ratedCalls = new ArrayList<String>();
ratedCalls = this.cdh.selectTopCalls();
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);
for (int i = 0; i < 1; i++) {
View item = inflater.inflate(R.layout.listitem, null);
TextView x = (TextView) item.findViewById(R.id.txt1);
x.setText(ratedCalls.get(0));
TextView ht = (TextView) item.findViewById(R.id.txt2);
ht.setText(ratedCalls.get(1));
ll.addView(item, ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
Your problem is that you should be using listitem.xml from a list adapter. What you're doing is, first setting your content view to an instance of listitem.xml, then trying to insert an instance of listitem INTO listitem's first LinearLayout. You should use listitem.xml as the view that you inflate in your custom adapter's getView() method. See the QuoteAdapter class from this question for an example, or just Google search "custom ArrayAdapter Android" for tons of results on this topic.
To work so you need to create two different xml for each inflater
In your layout folder,new xml layout/main2.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">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:src="#drawable/icon"
/>
<TextView
android:id="#+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="#+id/txt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
Then your layout/main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/linearId"
android:orientation="vertical"
>
<TextView android:id="#+id/rowCount" android:layout_width="wrap_content"
android:layout_height="wrap_content" ></TextView>
<LinearLayout
android:id="#+id/linearId2"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
</LinearLayout>
</LinearLayout>
Then you can set you activity like this
private int ELEMENTINEACHROW=2 ;
private int NOOFROW = 4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);
for (int j = 0; j < NOOFROW; j++) {
View itemMain = inflater.inflate(R.layout.main, null, false);
TextView rowCount = (TextView) itemMain.findViewById(R.id.rowCount);
rowCount.setText("Row:"+(j+1));
LinearLayout ll2 = (LinearLayout) itemMain.findViewById(R.id.linearId2);
for (int i = 0; i < ELEMENTINEACHROW; i++) {
View item = inflater.inflate(R.layout.main2, null, false);
TextView x = (TextView) item.findViewById(R.id.txt1);
x.setText("test");
TextView ht = (TextView) item.findViewById(R.id.txt2);
ht.setText("good");
ll2.addView(item, ViewGroup.LayoutParams.FILL_PARENT);
}
ll.addView(itemMain, ViewGroup.LayoutParams.FILL_PARENT);
}
}
Hope it give you the idea.
mmm...I did not understand fully what is required, but may be you need to add android:orientation="vertical" to LinearLayout with id="linearId".
In your xml file, set the android:orientation attribute to vertical on your <LinearLayout/>:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="#+id/linearId"
android:padding="6dip"
android:orientation="vertical"/>
Check out the LinearLayout class documentation and the orientation documentation for more information.

Categories

Resources