Initializing a view without findViewById() - java

I have an activity and a fragment. Now in the onCreate() of the fragment I want to add rows to my table layout defined in the xml of the fragment. For that I want to initialize a table layout to that defined in the xml. In the view of my application I only have the layout of the main activity set. I don't have the view of the fragment and I can't set it right now. So when I wrote this
TableLayout tableLayout = findViewById(R.id.record);
there is an error in the log cat. View can't be found. I understand the problem but I am not able to find its solution anywhere.
record is the id of the table layout in fragment's layout xml file
<TableLayout
android:id="#+id/record"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableLayout>
Thanks in advance.

By doing a lot of research I finally found the solution to my own problem.
For this I used a layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View view= inflater.inflate(R.layout.fragment_home_page, null);
TableLayout tableLayout = (TableLayout)view.findViewById(R.id.record);

Related

Change textview of another layout

I have a class "HomeActivity.class" and its layout "activity_home". I need to change the text of a TextView ("TV8") in another layout ("layout_profile"). I tried to use:
setContentView(R.layout.activity_home);
ConstraintLayout profile=(ConstraintLayout)findViewById(R.id.layout_profile);
TextView TV8 = (TextView) findViewById(R.id.TV8);
TV8.setText("MY TEXT IN TV8");
but my app crush when try to set text at line 4.
Maybe the problem can be the inflated layout: in activty_home layout i have a frame layout that inflate layout_profile where is my textview TV8:
activity_home:
layout_profile :
Debug application
HOW CAN I CHANGE THE TEXT OF A TEXTVIEW (TV8) IN A LAYOUT(layout_profile) FROM A CLASS(activity_home) THAT HAVE ANOTHER LAYOUT(activty_home)??
I think there is an issue in line 3.
Try this:
setContentView(R.layout.activity_home);
ConstraintLayout profile=(ConstraintLayout)findViewById(R.id.layout_profile);
TextView TV8 = (TextView) profile.findViewById(R.id.TV8);
TV8.setText("MY TEXT IN TV8");
You are loading activity_home.xml in the Activity and try to access the view TV8 which is not in activty_home.xml.
R.id.TV8 is part of layout_profile not part of activity_home.xml.
You can access those view which is in layout which is currently loaded in the screen.
Your activity is loading the activity_main layout so you can access the views that are present in the layout.
You are trying to access the view present in layout_profile which is not loaded and not visible. Until its loaded you can't access the view.
And if the view is not visible there is no point changing the text of that view.
Rather you can store the value in a flag and can be used to change the text when layout_profile is loaded.

Why does ListView need its container item in a sperate layout file

My questions stems from doing the tutorials for Android development, namely the Sunshine application. The specific code is here (a github pull request diff).
I have a ListView inside a FrameLayout in one layout XML file. Now, to use the ListView with an ViewAdapter (ArrayAdapter in my case) I need to have a layout specification for the container the adapter and ListView will utilize (a TextView in my case). Why does that container need to be in a separate layout file? (as presented in the github link)
I have attempted putting the TextView in the same layout file and changing the code appropriately, but it just crashes (I was not able to debug it successfully):
XML:
<FrameLayout 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: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=".MainActivity$PlaceholderFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView_forecast"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="#+id/list_item_forecast_textview"
/>
The relevant Java code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// create some fake data
String[] arrayList = {
"Today - Sunny - 35/30",
"Tomorrow - Very Sunny - 45/43",
"Today - Dangerous - 55/54",
"Today - Deadly - 62/60",
"Today - Boild an egg? - 100/93",
"Today - Radioactive fallout - 135/130",
"Today - Sunny side up - 150/130",
"Today - Burn - 4000/3978",
};
// pump it into something more managable
ArrayList<String> weatherList = new ArrayList<String>(Arrays.asList(arrayList));
// now create an adapter for the list view so it can feed them to the screen
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
weatherList);
// get the list view from the current activity
ListView listView = (ListView) rootView.findViewById(R.id.listView_forecast);
// finally set the adapter
listView.setAdapter(adapter);
return rootView;
}
The question appeared in an unclear form here - I hope I have rephrased it correctly.
The TextView in your layout is not a child of the ListView, it is a sibling. ListView manages its children based on how many items your adapter reports, how large each child (row) is, and how much space there is on screen to show them.
Regardless of whatever view you use for the row, each row needs its own instance of that view. Placing a view inside of a layout will cause one instance to be created and added to the view hierarchy, but ListView can have an arbitrary number of children depending on the data in your adapter. If your adapter has 3 items and each row is simply a TextView, then you need 3 TexViews to display them all, not just the one you added.
That's why it doesn't make sense to include the row layout anywhere in the same layout as the ListView.

Fragment not calling onDestroyView while parent layout removed

I have been stuck with a very strange problem with layouts & fragments.
I have been assigned with fixing an app that has really poor logic & design. It has single activity & all layout changes are done by simply removing content on wrapper (LinearLayout) and adding views to it.
I have been trying to inflate layout with < fragment > inside. And it works just fine. But when I change wrapper content (removeAllViews()) with different content and then try to reinflate layout with fragment again I get exception with:
12-18 18:28:36.711: E/AndroidRuntime(11192): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f07014d, tag null, or parent id 0x0 with another fragment for com.test.fragments.SearchTabhostFragment
While I remove parent layout (wrapper) - Fragment is not executing any of the lifecycle functions (onPause, onStop, onDestroyView etc).
Inflating & reinflating later on:
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.search, wrapper);
Destroying:
if (wrapper.getChildCount() > 0) {
wrapper.startAnimation(out);
wrapper.removeAllViews();
}
Search 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" >
<fragment
android:id="#+id/fragment1"
android:name="com.test.fragments.SearchTabhostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
</RelativeLayout>
Any idea what I might do to clear it from FragmentManager? Is there an event I might use on layout to find out it is being removed?

Adding xml widgets to a tab view with java

I have been following this guide and i have it running.
http://www.mkyong.com/android/android-tablayout-example/
I have added a bunch of new widgets to the tabs using java (image views and text fields) but its getting messy and i dont have as much control over the positioning and size as i would like to have since i cant use layouts
If i created a new layout using xml and i placed all my widgets perfectly using layouts etc is there a way i can populate a specific tab with that layout using java?
eg
rather than have code like this to add widgets
TextView textview = new TextView(this);
textview.setText("This is Android tab");
setContentView(textview);
i can just add a reference to an xml file and it will add all them widgets within the xml file
Use LayoutInflater http://developer.android.com/reference/android/view/LayoutInflater.html
LayoutInflater inflater = LayoutInflater.from(context); // if you're inside of activity then context = this
View viewInflatedFromXml = inflater.inflate(R.layout.your_layout, null);
Then you can do what you want with this view.

convert android UI xml to code

So, I'm trying to make a dynamic UI, and i want to add a seperator to it. unfortunately, i could only find out how do one in XML. is it possible to turn this
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/seperator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:scaleType="fitXY"
android:src="#android:drawable/divider_horizontal_dark" />
into program code?
my best attempt was
ImageView seperator=new ImageView(this);
seperator.setImageDrawable(drawable.divider_horizontal_dark);
Put it in an extra layout file and inflate it when you need it in code - I think that what you want to do and should be the easiest way.
In your Activity:
View v = LayoutInflater.from(this).inflate(R.layout.seperator, null);
If you inflate a Layout:
LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_layout, null);
TextView tv = (TextView) ll.findViewById(R.id.tv);
There is a website can convert that for you. You can design the interface with eclipse then submit the generated xml to XMLtoJAVA online converter and it should do it for you..
You can also create a View, define a background and add it with a LayoutParams
ViewGroup container = (ViewGroup) findViewById(R.id.container);
View separator = new View(context);
separator.setBackgroundColor(Color.Black);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 2);
container.addView(separator, layoutParams);

Categories