So I've been trying to figure out layouts and using the layout inflater, but I'm running into some issues. I have two relative xml layout files, the one xml file has two textViews, an editText field, and a spinner object:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/goalNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dip"
android:text="#string/goalName" />
<EditText
android:id="#+id/goal_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/goalNameView"
android:hint="#string/editText"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/goalTasksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/goal_tasks"
android:layout_alignBottom="#+id/goal_tasks"
android:layout_alignLeft="#+id/goalNameView"
android:text="#string/goalTasks" />
<Spinner
android:id="#+id/goal_tasks"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/goalTasksView"
android:layout_marginTop="51dp" />
</RelativeLayout>
the other xml file has one textView and one editText field:
<RelativeLayout 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:id="#+id/tView"
>
<EditText
android:id="#+id/taskNameField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/taskNameView"
android:ems="10"
android:hint="#string/editText" />
<TextView
android:id="#+id/taskNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/taskNameField"
android:layout_alignBottom="#+id/taskNameField"
android:layout_alignParentLeft="true"
android:text="#string/taskName"
/>
</RelativeLayout>
I'm trying to patch the two layouts together with a third xml file (main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/theMain"
>
<include android:id="#id/mainLayout" layout="#layout/activity_goal_keeper" />
</LinearLayout>
The way I'm looking to have the layout work is to display the first layout (#id/firstLayout) and then the second layout (#id/tView) directly beneath it. I've read that I need to implement the layouts on a LinearLayout to achieve this which is why I have the third layout (#id/theMain).
As you can see I have an include statement for #id/firstLayout, but I don't have an include statement for #id/tView because I'm trying to inflate multiple versions of #id/tView via my main activity:
public class GoalKeeperActivity extends Activity implements AdapterView.OnItemSelectedListener {
public Integer[] items= {1,2,3,4,5,6,7,8};
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.activity_goal_keeper);
setContentView(R.layout.main);
Spinner numOfTasks=(Spinner) findViewById(R.id.goal_tasks);
numOfTasks.setOnItemSelectedListener(this);
ArrayAdapter<Integer> aa = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, items);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
numOfTasks.setAdapter(aa);
ViewGroup item = (ViewGroup) findViewById(R.id.theMain);
for(int i=0; i<3;i++)
{
View child = getLayoutInflater().inflate(R.layout.tasks_view, item, false);
child.setId(i);
item.addView(child);
}
My problem is that #firstLayout displays properly, but #tView doesn't show up at all. Can someone please explain what I'm missing?
Thanks in advance.
I figured it out, it was simply an issue of setting the "android:layout_height" attribute equal to wrap_content on the #id/tView xml file. Silly little mistakes...
Related
I am using the following to add EditTexts along with cancel buttons dynamically to my main activity.
JAVA
LinearLayout root = (LinearLayout) findViewById(R.id.root_layout);
for(int i=0;i<numberOfEditTexts;i++)
{
LinearLayout editText = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.edittexts,root,false);
editText.setId(View.generateViewId());
root.addView(editText);
}
edittexts XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ems="10"
android:hint="#string/hint_email_id" >
<requestFocus />
</EditText>
<ImageButton
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/cancel_button"
android:onClick="deleteRow"
android:layout_gravity="center"/>
</LinearLayout>
I am already using generateViewId(). Does that ensure unique ids?
More importantly, I want to get/set texts individually on all dynamically created EditTexts. How can I access them?
I'm setting a list adapter as follows, and if i just change simple_list_item_activated_1 for lv_layout it doesn't work at all.
any ideas?.
Thanks!
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
DummyContent.ITEMS));
}
How can I attach my custom layout called lv_layout to that adapter?
this is my layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/lv_layout"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />
<TextView
android:id="#+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
<TextView
android:id="#+id/resolution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
Why you want to use setlistadapter then? You have to create your custom adapter too in that case because serlistapater will only update the textview part of your custom layout and rest part it will ignore which idont think what you want. So
1.make a customadapter
2.override its getview
3.the set the adapter to your custom layout.
Cheers
android.R.* are Android's internal resources.
To use your own layouts from res/layout/ skip the android prefix:
R.layout.lv_layout
Same goes for ids (if you're wondering).
I'm new to Android development.
I have Activity, to which I wish to add some GUI-elements.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dial_details);
if (android.os.Build.VERSION.SDK_INT >= 11)
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
inflateNewRow();
inflateNewRow();
}
private void inflateNewRow()
{
LinearLayout thisLayout = (LinearLayout) findViewById(R.id.dial_details_layout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Before edit!
//inflater.inflate(R.layout.dialitem_details, thisLayout);
// After edit, which works!
inflater.inflate(R.layout.dialitem_details, null);
thisLayout.addView(ll);
}
The first call to inflateNewRow adds the row, fine. The second call, seems to do nothing! No exception, no row is added.
I added the setVisibility call, just to make sure. It makes no difference.
It might seem strange to call inflateNewRow twice, but it's actually just for this example. It is to be called once in the onCreate, then from a menus click-event. This simple example recreates the problem though.
This is the little view that's to be added:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
into this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dial_details_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DialDetailsActivity" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
Any ideas why only the first row appears / is added?
when you call this function once LinearLayout ll is created once but when you call again it already exists isn't it?
so if you do not create ll and just write
inflater.inflate(R.layout.dialitem_details, thisLayout);
instead of
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.dialitem_details, thisLayout);
you should get desired result
and there is no need to set visibility true for each item
The answer to this question might be really obvious but its giving me a headache. I have a simple LinearLayout with one single ListView in it. I do this: onCreate
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.friends);
ListView listView = (ListView) findViewById(R.id.friend_list);
listAdapter = new CheckinListAdapter(checkins, listView, R.layout.checkin_list_item);
listView.setAdapter(listAdapter);
if (getLastNonConfigurationInstance() != null) {
FriendsActivity last = (FriendsActivity) getLastNonConfigurationInstance();
this.checkins.addAll(last.checkins);
this.sort = last.sort;
} else {
refresh();
}
registerForContextMenu(listView);
}
But for some reason onCreateContextMenu never gets called! So I did some research and since I am loading the list after the register, perhaps it doesn't register it correctly. If I go in my ListAdapter and do registerForContextMenu it does show up. But it doesn't behave correctly with the keyboard. So I am now confused on what can be the error because it seems a little non intuitive for me to have to register each child item. All the examples I find online are using ArrayAdapter. :(
Any suggestions?
Edit
Here is more detail, in case it something I don't see:
My XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="#string/check_in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onCheckInClicked"/>
<ListView android:id="#+id/friend_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
List item xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<ImageView android:id="#+id/user_photo"
android:layout_width="40dip"
android:layout_height="40dip"
android:scaleType="centerCrop"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="8dip">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="#+id/user" style="#style/TextButton"/>
<TextView android:text="#string/at"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="#+id/venue"
android:singleLine="true"
android:ellipsize="end"
style="#style/TextButton"/>
</LinearLayout>
<TextView android:id="#+id/venue_address" style="#style/GreyLarge"/>
<LinearLayout android:id="#+id/checkin_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip">
<ImageView android:id="#+id/checkin_image"
android:layout_width="70dip"
android:layout_height="60dip"
android:layout_marginRight="8dip"
android:scaleType="centerCrop"/>
<TextView android:id="#+id/checkin_shout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView android:id="#+id/elapsedTime" style="#style/GreySmall"/>
</LinearLayout>
</LinearLayout>
This took me 6 hours to figure out but it turns out I had to add:
android:focusable="false"
to all my <Button/> tags.
Related post: TextView and Button in each row and onListItemClick()
Do, registerForContextMenu(listView);
before setting the adapter, that is before:
listView.setAdapter(listAdapter);
You have mentioned that the menu is not responding correctly with the keyboard actions. Can you tell me what exactly the problem is?
I have following main layout:
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<ViewFlipper android:id="#+id/viewstack"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Here I want to add my views which are located in separated xml files. -->
</ViewFlipper>
</LinearLayout>
Here is example of my view:
view_url.xml
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:gravity="center">
<EditText android:text="#+id/EditText01"
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnGenerate"
android:text="Generate"/>
</LinearLayout>
view_text.xml
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<EditText android:text="#+id/EditText01"
android:id="#+id/EditText01"
android:layout_height="wrap_content"
android:contentDescription="Enter your text here"
android:layout_width="fill_parent"
android:height="200dp"/>
</LinearLayout>
I am trying to add views:
viewstack = (ViewFlipper) findViewById(R.id.viewstack);));
View viewText = (View) findViewById(R.layout.view_text);
viewstack.addView(viewText); < -- Emulator is crashing at this line
View viewUrl = (View) findViewById(R.layout.view_url);
viewstack.addView(viewUrl);
I dont have any idea what is wrong with my code. I decided to put all my views in one file, but I still want to know how to fix my initial code.
Yout View viewUrl = (View) findViewById(R.layout.view_url); is very wrong. findViewById is kinda like the get(String key) method the the directory where your directory is your current view/activity. It only looks up the element with that Id under it's children.
To create Java objects out of XML files you need use you need to use the LayoutInflater. Which is pretty straight forward, out of that you get the object you can pass to viewstack.addView(..) method.
Another way to achieve this would be to just include the other XML files into the first one by using either the include, merge tags, or the ViewStub. Depending on your requirements these might not be an option though, but what you are describing they should be, and you should use these instead of doing it programatically because it's just cleaner this way.
maybe this help:
this.flipper=(ViewFlipper)findViewById(R.id.flipper);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.viewLoader=(View)inflater.inflate(R.layout.search_result_grid, null);
flipper.addView(viewLoader);
this.viewResultGrid=(View)inflater.inflate(R.layout.search_result_grid, null);
gvSearchResult=(GridView)viewResultGrid.findViewById(R.id.gridViewSearchResult);
flipper.addView(viewResultGrid);
It's name suggests that It will find by id not by layout.You should understand difference between layout and id.use like this one View v=(View)findViewById(R.id.your_view_id);
Easiest way to add by inflating View. Here some small snippet where you can find reference.
private View viewText;
private TextView txtPost;
private void setViewFlipperPost(String postData, String postType) {
if (postType.toLowerCase().toString().equals("text")) {
viewText = LayoutInflater.from(mContext).inflate(R.layout.activity_full_screen_textpost, null, false);
viewText.setTag(TAG_TEXT);
txtPost = (TextView) viewText.findViewById(R.id.txtTextPostFullScreenText);
txtPost.setText(postData);
viewFlipper.addView(viewText);
}
}
use viewflipper
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/white"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/view_flipper"
android:layout_width="match_parent"
android:layout_height="410dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="first layout"
android:textColor="#845965"
android:textSize="25dp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="second layout"
android:textColor="#654123"
android:textSize="25dp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
//you can add many layout here
</viewFlipper>
<Button
android:id="#+id/flipbyclickNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bn1"
android:onClick="flipByClickNext" />
<Button
android:id="#+id/flipbyclickprevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="flipByClickPrevious"
android:background="#drawable/bp1" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private ViewFlipper viewFlipper;
//Button next,prev;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
}
public void flipByClickNext(View v)
{
if(viewFlipper.isFlipping())//Checking flipper is flipping or not.
{
viewFlipper.stopFlipping(); //stops the flipping .
}
viewFlipper.showNext();//shows the next view element of ViewFlipper
}
public void flipByClickPrevious(View v)
{
if(viewFlipper.isFlipping())//Checking flipper is flipping or not.
{
viewFlipper.stopFlipping(); //stops the flipping .
}
viewFlipper.showPrevious();
}
}