How to know the View Elements after loading a layout? - android

I am trying to write code , where after loading the Layout , i want to know which are the view elements are related to the layout like TextView, EditText , Checkbox etc.
MyCode :
MainActivity.java
package com.example.myview;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
// How to get to know about the UI Elements related to this layout //
}
}
demo_layout.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"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lnr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ddccee"
android:orientation="vertical">
<TextView
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="15dp"
android:textColor="#ffffff"
android:text="Test Layout"
android:layout_gravity="center"
android:gravity="center"/>
<EditText
android:id="#+id/edit1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="15dp"
android:textColor="#ffffff"
android:text="Test Layout"
android:layout_gravity="center"
android:gravity="center"/>
<TextView
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="15dp"
android:textColor="#ffffff"
android:text="Test Layout"
android:layout_gravity="center"
android:gravity="center"/>
</LinearLayout>
This layout has TextView , EditText , so , i want to know how to get UI elements are related to this layout programmatically.
So , please suggest me some solution.

You can use getChildCount REF to get the count of views added toViewGroup and then use getChildAt REF to get the View at given index.
You are using LinearLayout as base layout which already extends the ViewGroup Just change below line.
LinearLayout view = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.demo_layout, null);

After you've inflated your demo_layout just call findViewById on your view instance like this:
View view = LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
TextView txt1 = (TextView) view.findViewById(R.id.txt1);

Elements of view must be retrieved by view.findViewById().
Except in activity you use findViewById() directly to retrieve view elements of the contentView you set in setContentView().

Related

I want to replace one fragment with another but these should happen based on button clicked in some other fragment(main one)

Front endI have three fragment one will be the main fragment and the other two fragments for some tasks, I want to display the remaining fragment by clicking the buttons in main fragment but the main fragment should not be replaced so that if I click the other button the previous one should be replaced by the the new button , this should continue based on the buttons I click
enter code here
MainActivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/headline_fragment"
android:name="com.example.fragments.headlinefragment"
android:layout_weight="1"></fragment>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/baseline"
android:name="com.example.fragments.baseline"
android:layout_weight="1"></fragment>
</LinearLayout>
fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
android:textSize="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="headline" />
</android.support.constraint.ConstraintLayout>
similarly for other two
fragment.java:
package com.example.fragments;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class headlinefragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.headline_fragment,container,false);
return view;
}
}
-similar for other two
ok this isnt a great solution, but you can just show or hide the view using set visibility, the actual way to do this is to inflate your fragment using a tag and then check if its in the layout has been added etc before deciding whether to show or remove it, but if your only showing a small fragment that doesnt do much you can include it in your layout like you have already except inside a frame layout and then call fragmentHolder.setVisibility(View.VISIBLE); or View.INVISIBLE to toggle it on and off

findViewById() from another layout not updating data

I have mainActivity, that has xml that include another xml . Both activitymain layout has a button, and the included layout has another button. Both should display into the main activity.
Now , i need to change text of both button. I can get reference to first button by findviewByID , and it works. for the second button that is into the second layout, i get view by inflating the layout, It gets properly refence to the view and the findviewbyID gets reference to this button. But when changing the text, it does not change.
activityMain.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="match_parent"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools">
<Button android:id="#+id/buttonvvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
xmlns:android="http://schemas.android.com/apk/res/android" />
<include
layout="#layout/layouttest"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
layouttest.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">
<Button
android:id="#+id/buttonaaa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
---Mainactivity.java----------------------------------
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view=LayoutInflater.from(getApplication()).inflate(R.layout.layouttest, null);
Button b =(Button) findViewById(R.id.buttonvvv);
Button bb =(Button) view.findViewById(R.id.buttonaaa);
b.setText("ssssssss");
bb.setText("ssssssss");
}
Results
buttonvvv text is updated and becomes ssssssss
buttonaaa text is Not updated and still Button
No need for inflating the included layout, you can just findViewById like you did with b button. It's already included with activity_main.xml layout
Replace this:
View view=LayoutInflater.from(getApplication()).inflate(R.layout.layouttest, null);
Button b =(Button) findViewById(R.id.buttonvvv);
Button bb =(Button) view.findViewById(R.id.buttonaaa);
b.setText("ssssssss");
bb.setText("ssssssss");
With :
Button b =(Button) findViewById(R.id.buttonvvv);
Button bb =(Button) findViewById(R.id.buttonaaa);
b.setText("ssssssss");
bb.setText("ssssssss");
From Docinclude:
The root View should be exactly how you'd like it to appear in each
layout to which you add this layout.
BTW, check this answer
You don't have to inflate the layout, just find it like:
LinearLayout layout =(LinearLayout) findViewById(R.id.layouttest);
and then
Button bb =(Button) layout.findViewById(R.id.buttonaaa);

Layout glitch: Nesting RelativeLayout inside a LinearLayout

Problem: I want to clarify a very basic layout issue. I have been at it for 2 days to no avail. I simply want to successfully nest a RelativeLayout inside a LinearLayout and control its positioning. For this purpose, I have created 2 layouts: 1 layout to be displayed on the left and one on the right.
I am getting the following output:
You can see the second view should be on displayed on the right but its not. I have tried layout_gravity but its not working.
Following are the layouts:
For left:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/message_view_border">
<!--The message text-->
<TextView
android:id="#+id/message_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<!--the timestamp-->
<TextView
android:id="#+id/time_stamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/message_content"
android:textStyle="bold"/>
</RelativeLayout>
For right:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="#drawable/message_view_border"
android:layout_gravity="right"
android:gravity="right"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<!--The message text-->
<TextView
android:id="#+id/message_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<!--the timestamp-->
<TextView
android:id="#+id/time_stamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/message_content"
android:textStyle="bold"/>
</RelativeLayout>
Here is the Activity that's working the scenario:
package com.example.testapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Activity1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
//Get handle to the root view's child. i.e, FrameLayout
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout linearLayout = (LinearLayout) viewGroup.findViewById(R.id.container_linear_layout);
View message_receiver = inflater.inflate(R.layout.layout_chat_message_receiver, viewGroup, false);
TextView received_content = (TextView)message_receiver.findViewById(R.id.message_content);
received_content.setText("okay");
linearLayout.addView(message_receiver);
View message_sender = inflater.inflate(R.layout.layout_chat_message_sender, viewGroup, false);
TextView sent_content = (TextView)message_sender.findViewById(R.id.message_content);
sent_content.setText("okay");
linearLayout.addView(message_sender);
}
}
Project for your reference: https://github.com/mankum93/TestApp1
Note: I know that I have asked a similar question with no answer to it. I am asking this again because people questioned in the question about the RecyclerView I was using to populate a dynamic list of views. That may have been the reason that no one could identify the root cause of the problem. But since, the problem is a fundamental one which exists even without a RecyclerView, I think, it deserves another chance.
I have searched several forums, docs, and SO posts but can't resolve this problem.
EDIT 1: I can see the source of confusion here:
Here it the main activity layout file:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/container_linear_layout">
</LinearLayout>
<!--Floating view to send messages layout-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<EditText
android:id="#+id/typed_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND"
android:layout_gravity="bottom"
android:layout_weight="0"/>
</LinearLayout>
</FrameLayout>
The problem is with the way you are inflating your Views:
inflater.inflate(R.layout.layout_chat_message_sender, viewGroup, false);
The second parameter to inflate() is a root ViewGroup, which is the parent of the View you are inflating. You are passing the third parameter (attachToRoot) as false, but the LayoutInflater will still use that root ViewGroup to generate a set of LayoutParams.
In your case, you are passing the root FrameLayout of your activity as the root to the inflater, when in reality you are going to be attaching your views to the LinearLayout.
Changing those inflation lines like so will fix your problem:
inflater.inflate(R.layout.layout_chat_message_sender, linearLayout, false);

Listview in layout Android

I have a string-array, that i need to display in a layout.
The code currently shows the list, but the list isn't in the layout, its make a new "screen" where the only thing you can see is the list.
Here's the code that i just can't get to show the list in the layouts listview.
import android.app.ListActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class idchart extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.idchart);
Resources res = getResources();
// To get the string array associated with particular resource ID.
String[] ids = res.getStringArray(R.array.idchart_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.test_list_item,ids);
setListAdapter(adapter);
}
}
layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_main"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/logo" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<RelativeLayout
android:id="#+id/relative"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
</LinearLayout>
I haven't been able to find anything that helped :( so i hope you will be able to.
ListView listView=(ListView)findViewById(R.id.lv);
//rest of code
listView.setAdapter(adapter);
And you can avoid extending ListActivity.
If you are using ListActivity then
ListView listView=getListView();
Your idchart class should be a subclass of a regular Activity instead of a ListActivity which are for only displaying lists.
Update
Replace
public class idchart extends ListActivity
with
public class idchart extends Activity
If you extend ListActivity, your ListView must have a specific id:
android:id="#id/android:list"
From the doc:
ListActivity has a default layout that consists of a single, full-screen list in the
center of the screen. However, if you desire, you can customize the screen layout by
setting your own view layout with setContentView() in onCreate(). To do this, your own
view MUST contain a ListView object with the id "#android:id/list"

Replication of Apple's Search in Android

I want to create a UI similar to as shown here http://appsreviews.com/wp-content/uploads/2010/08/Cures-A-Z-App-for-iPhone.jpg
I started out with trying to put two custom lists side by side like in this code
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class Emp extends Activity {
/** Called when the activity is first created. */
private String tableName = DBHelper.tableName;
private SQLiteDatabase newDB;
public static WeakHashMap<String, Empbook> temp = new WeakHashMap<String, Empbook>();
final List<Empbook> listOfEmpbook = new ArrayList<Empbook>();
final List<String> listOfAlphabets = new ArrayList<String>();
TextView txt;
EmpbookAdapter adapter = new EmpbookAdapter(this, listOfEmpbook);
Integer pos;
Integer count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt=(TextView)findViewById(R.id.textView1);
ListView list = (ListView) findViewById(R.id.ListView01);
ListView alist = (ListView) findViewById(R.id.ListView02);
list.setClickable(true);
alist.setClickable(true);
AlphabetListAdapter alphabetadapter = new AlphabetListAdapter(this,
listOfAlphabets);
list.setAdapter(adapter);
alist.setAdapter(alphabetadapter);
the alphabetadapter is for the list displaying alphabets on the right in the screen.
My XML is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="TextView" android:id="#+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<ListView android:id="#+id/ListView01" android:layout_width="280dp"
android:orientation="vertical" android:layout_height="wrap_content"></ListView>
<ListView android:id="#+id/ListView02" android:layout_width="wrap_content"
android:orientation="vertical" android:paddingLeft="282dp"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
The problem that's occurring is that only one view at a time(the one which is put earlier in the above xml is displayed while the other just doesn't appear).
Can someone please push me in the right direction?
EDIT: I tried to set the weights of the lists setting one to zero and setting the other to 1,it works partially now i can see both lists however one of the list isn't getting populated....will update if i work it out.
Posted an answer below (One listview dropped though.) Check it out.
If the index on the side is what you're looking for, you should try this: http://hello-android.blogspot.com/2010/11/sideindex-for-android.html
If you want to add to elements side by side which together fill their parent:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListView01"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="90"
android:background="#FFFF0000"/>
<ListView
android:id="#+id/ListView02"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10"
android:background="#FF00FF00"/>
</LinearLayout>
You had "wrap_parent" as the height of the second element. If it wasn't being filled properly it would have the height of 0 - I've changed it to match parent. I've also added a system for using "percentage" filling.
Also, all other "fill_parent" tags I've changed to "match_parent" - not because it changes the functionality of the code but because "fill_parent" is deprecated because as a label it is misleading.
Also, I've added a background to the elements which will more helpfully debug where your problem is.
I would also suggest that what you should be aiming for is infact one View (NOT a ListView even though I have kept it for this example) which would be placed above the other (Just as the Apple search has their alphabet):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="#+id/ListView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF0000"/>
<!-- Since the contents of the view don't change it seems wasteful to create this as a listview -->
<ListView android:id="#+id/ListView02"
android:layout_width="20dp"
android:layout_height="match_parent"
android:background="#FF00FF00"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"/>
</RelativeLayout>
Found a work around now use a textview and a listview nested in a framelayout like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:cacheColorHint="#00000000" />
<TextView android:id="#android:id/empty"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:text="textview" />
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:background="#android:color/transparent" android:id="#+id/sideIndex"
android:paddingLeft="280dip"
android:layout_width="300dip" android:layout_height="fill_parent"
android:gravity="center_horizontal">
<TextView android:text="T" android:id="#+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</FrameLayout>
More can be found out here http://dotslasha.in/wp/143/creating-floating-views-in-android .
Ty and Cheers !! :)
You don't need to implement this yourself, Google has helpfully given you API to use their search functionality.
The documentation on the subject should be enough to get you from start to finish. It's available here: http://developer.android.com/guide/topics/search/search-dialog.html
In the second ListView you have got one big padding: android:paddingLeft="282dp". I assume you are not coding for tablets in landscape only, so just remove the padding-attribute.
Remove the text view which is the first element (you can replace this by using addHeaderView() or wrapping this linearlayout onside a vertical one).
Look carefully at how the width and height are set in the following code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListView01"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
></ListView>
<ListView
android:id="#+id/ListView02"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
></ListView>
</LinearLayout>
In my experience, the weight will only work properly if the width is set to wrap_content.

Categories

Resources