Change textview of another layout - java

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.

Related

Android - Get Other Layout Widget

I've been trying to get a widget from another layout for past hours but still no luck. Can someone tell what is wrong with my current code?
public void refreshClicked(View view){
tab1 = LayoutInflater.from(view.getContext()).inflate(R.layout.tab_1, null);
Button b = (Button) tab1.findViewById(R.id.refresh);
b.setText("Updating...");
}
I've hooked my button onClick to refreshClicked(View view) method.
This method is under my MainActivity.java and I'm trying to get the Button with id refresh from tab_1.xml layout but the button text is not updating.
Thanks!
You seems to inflate a layout but never attach it to a parent view as the ViewGroup parameter is null. Then this layout inflated must not even been displayed anywhere.
If the button b already exists, no need to inflate the layout and retrieve the button form there, it will a different button.
Just retrieve the button from the actual existing layout.

Initializing a view without findViewById()

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

How to remove defaut icon/title from top of main layout in Android?

Check out this picture:
WhoKnew title and WK? Icon http://puu.sh/5Ejo5.png
See the icon and title there? How do I go about removing that?
I'm not using any of the drag and drop or xml editing at all in eclipse for this project, because I have to dynamically create an arbitrary amount of buttons/text, and whatnot.
Basically, I've been going about it by doing something like this..
public createMainMenu(){
//make layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(backgroundColor);
//make/add text to layout
menuText.setText("Who Knew?");
menuText.setId(20001);
menuText.setTextSize(36);
menuText.setTextColor(textColor);
layout.addView(menuText);
//make a scrollview, then add the layout to the scrollview
ScrollView sc = new ScrollView(this);
sc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
sc.setFillViewport(true);
sc.addView(layout);
//set content view to the scrollview
setContentView(sc);
}
Hopefully, I'm on the right track here on how I should be going about this. The app I have is working fine, I just need to get rid of that icon/title bar, if possible. I guess it's not a gamebreaker, but it's a bit annoying.
you can use this in your java class
requestWindowFeature(Window.FEATURE_NO_TITLE);
Add this to your manifest:
android:theme="#android:style/Theme.Light.NoTitleBar"

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.

findViewById on Custom Title Bar within Tab Activity

I have a custom title bar set on my TabActivity. The custom title bar contains a TextView and an ImageView in it. The tabHost has multiple tabs.
What I want to do is to access the ImageView resource in the tabs.
I am accessing the TextView from custom title bar in the main Tab activity (that extends TabActivity) and it is working fine. Following is the code which is working fine in main activity:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.myactivity);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mycustomtitle);
TextView tv = (TextView) findViewById(R.id.viewTitleId);
Now I want to access the ImageView in my tabs (which are added in tabHost). If I set following code in tab:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
It gives following error:
You cannot combine custom titles with other title features
And if i directly set following in the tab code:
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mycustomtitle);
ImageView image = (ImageView) findViewById(R.id.imageTitleId);
The image remains null.
mycustomtitle.xml has following two elements:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.80"
android:id="#+id/viewTitleId"
android:textColor="#color/titleBarTextColor"
android:gravity="center_vertical"
android:text="#string/viewText"
android:textSize="18px"
android:paddingLeft="45px"
/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.20"
android:src="#drawable/btn_save"
android:id="#+id/imageTitleId"
>
</ImageView>
Please give me some idea how can I access the Custom title's ImageView in the tabs ?
You can access the TextView and ImageView by declaring them public static in the TabActivity. Then, in the Sub-Activity you obviously access the public static TextView and ImageView like,
Main_Tab_Activity.textView.setText("my_text_view");
If you put Custom Title bar only in Main TabActivity that Title bar will appears in all of your Sub-TabActivity.
For Example if you gave Custom title Bar in Main TabActivity :
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.header_layout);
TextView tv = (TextView) findViewById(R.id.viewTitleId);
ImageView image = (ImageView)findViewById(R.id.imageTitleId);
there is no need to give in all you Sub-tab Activity.
In all sub-Tab Activity you just set setContentView(R.layout.sub_layout);
use
getParent().getWindow().findViewById(id)
insted id findViewById(id);
for access any view of parent.

Categories