Inflating GUI works only once, how come? - android

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

Related

Setting focus on LinearLayout

I am trying to set focus on a LinearLayout view when returning to an activity from another activity. The scenario:
I have one activity with a bunch of LinearLayout items (with stuff in them), some of which are text, others photos. Let's say the top most of 20 is a TextView and the rest are photos. When I leave this activity (to take a picture), activity goes to the top of the list where the TextView is - no matter what I do.
How do I force the focus back on the item that I was on before the new activity was triggered?
I have already tried this with no success:
[How Linear Layout get Focus?
I am currently trying to force the focus by remembering the previous view without success (though I can see the code being executed). The in my main activity:
#Override
public void onResume() {
super.onResume();
// See if we had a field with focus previously...
if (mPreviousView != null) {
if (mPreviousView.isFocusable()) {
mPreviousView.requestFocus();
}
}
}
...
#Override
protected void onPause() {
super.onPause();
mPreviousView = this.getCurrentFocus();
}
I am dynamically setting the focus in the LinearLayout:
private void initialize() {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.field_photo_picker, this);
// To allow focus to be returned to control after taking photo
this.setFocusableInTouchMode(true);
this.setFocusable(true);
this.setClickable(true);
And finally the XML for the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="normal"
android:text="" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/photoValue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="17sp"
android:textStyle="normal"
android:gravity="right|center_vertical"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:text="" />
<ImageButton
android:id="#+id/photoPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/form_photo"
android:padding ="5dp"
android:gravity="center_vertical"
android:focusableInTouchMode="false"
android:background="?android:attr/selectableItemBackground"
android:layout_weight="0"
/>
</LinearLayout>
<View
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey" />
</LinearLayout>
</merge>
make your textview and other views focusable, you made this.setFocusable(true); which doesn't make sense

EditText steals focus from other EditTexts inside ListView

I have a stand-alone EditText and a ListView. Each item in the ListView also has two EditText fields. When I touch inside the ListView EditText, the stand-alone EditText steals the focus. This makes it impossible to edit any of the ListView EditText fields.
Name steals the focus from the other TextView elements inside the ListView
Here is the activity's XML, containing the stand-alone EditText and the ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dip"
android:orientation = "vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="16dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dip"
android:layout_weight="1"
android:text="#string/name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editblindschedule_name"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="5"
android:ems="10"
android:inputType="text" />
</LinearLayout>
<ListView
android:id="#+id/listview_editblindschedule"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And here is the ListView row XML:
<?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" >
<TextView
android:id="#+id/editblindschedule_round"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<EditText
android:id="#+id/editblindschedule_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/small"
android:ems="4"
android:inputType="number"
android:layout_weight="4"
android:layout_marginRight="10dip"
android:text="#string/integer_zero"
android:gravity="center" />
<EditText
android:id="#+id/editblindschedule_big"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/big"
android:ems="4"
android:inputType="number"
android:layout_weight="4"
android:layout_marginRight="10dip"
android:paddingRight="6dip"
android:text="#string/integer_zero"
android:gravity="center" />
<CheckBox
android:id="#+id/editblindschedule_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true" />
</LinearLayout>
Here is the Java code
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_blind_schedule);
...
adapter = new BlindAdapter(this, blindSchedule.getBlindLevels());
listView = (ListView) findViewById(R.id.listview_editblindschedule);
listView.setAdapter(adapter);
And finally, the adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.listview_editblindschedule, parent, false);
if (!creating) { // Set default values.
TextView round = (TextView) row.findViewById(R.id.editblindschedule_round);
EditText small = (EditText) row.findViewById(R.id.editblindschedule_small);
EditText big = (EditText) row.findViewById(R.id.editblindschedule_big);
round.setText("" + (position + 1));
small.setText("" + blindLevels.get(position).getSmallBlind());
big.setText("" + blindLevels.get(position).getBigBlind());
}
return row;
}
The solution that worked for me was to put android:windowSoftInputMode="adjustPan" in the activity in the manifest. This was the only change necessary for me.
I don't like it but after digging ListView code I ended up by overriding the getFocusedChild() method and returning null as all other trials of flag combinations didn't get to a satisfactory solution.
Even if android:windowSoftInputMode="adjustPan" works adjusting pan leads to too much artifacts in my layout.
Beware overriding that method may lead to other unwanted behaviours so use it and test it comprehensively in all android versions you support and may be call super instead of returning null for android versions that does not/will have this problem.

setText not displaying value

One of my textViews is not being adjusted when I call the function:
public void wordList() {
setContentView(R.layout.activity_main);
TextView lv = (TextView) findViewById(R.id.listText);
lv.setText("Text");
}
Here's the xml:
If I add android:text="Text" to the xml it works.
<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"
tools:context=".MainActivity"
android:gravity="center_horizontal"
android:background ="#268496" >
<LinearLayout android:id="#+id/linear"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/prefixText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textIsSelectable="true"
android:textSize="12pt"
android:typeface="sans" />
<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:textSize="12pt"
android:maxLength="1"
android:typeface="sans" />
</LinearLayout>
<TextView
android:id="#+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/linear"
android:textColor="#FFFFFF"
android:textIsSelectable="true"
android:textSize="12pt"
android:typeface="sans" />
</RelativeLayout>
If you call setContentView() after running it here then it will overwrite this call and set the content to its default (what's in the xml). If you call setContentView() say in onCreate() then you don't need to call it again as long as the TextView is inside that xml.
So, call setContentView() in onCreate() then call your function
wordList();
then
public void wordList() {
TextView lv = (TextView) findViewById(R.id.listText);
lv.setText("Text");
}
You have white color of textview's text. So if the background color is white, then you just don't see it.
You're not doing anything "wrong" in your posted example. The kinds of things that may be in play here are:
a) is your TextView within the visible area (hard code some text to see if it shows up)
b) are you sure you're calling the method that sets the text?
c) do you have some other overlapping ID somewhere else that is confusing this process?
It has to be something like that.

Why aren't both of my views being displayed?

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...

How to programmatically add view in ViewFlipper

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

Categories

Resources