This seems like it should be simple, but I can't figure out a way to do it. I'm needing a tab to have beginning text, but then have that text change after the user selects an item from a list. I know how to change tab backgrounds and colors via
mTabHost.getChildAt(index).setBackgroundColor();
but there isn't an option to change the tab's indicator. I've tried using an EditText.
private EditText tabName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statistics);
comm = new Communicator();
tabName = new EditText(this);
tabName.setText("BeginningText");
mTabHost = getTabHost();
mTabHost.addTab(mTabHost
.newTabSpec("tab_1_stat")
.setIndicator(User)
.setContent(R.id.meStatsTab));
mTabHost.addTab(mTabHost
.newTabSpec("tab_2_stat")
.setIndicator(tabName.getText())
.setContent(R.id.themStatsTab));
mTabHost.addTab(mTabHost
.newTabSpec("tab_3_stat")
.setIndicator("Archive")
.setContent(R.id.archiveStatsTab));
mTabHost.setOnTabChangedListener(this);
getTabWidget().getChildAt(1).setOnClickListener(new onThemTabClicked());
mTabHost.setCurrentTab(0);
onTabChanged("tab_1_stat");
}
.....
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
tabName.setText("ChangedTabText");
Bundle extras = intent.getExtras();
themStats = extras.getStringArray("themStats");
mTabHost.setCurrentTab(1);
onTabChanged("tab_2_stat");
}
That didn't work either, along with a few other attempts. Any ideas? Thanks ahead of time!
try this easiest way which works for me :
tabLayout.getTabAt(index).setText("TabName");
Wow. Okay this was a pain. Apparently TabWidget does something funky with RelativeLayout and everytime I tried to do anything with radek-k's solution it was blowing up with a RelativeLayout error. So basically the work around is the following. It also allows you to change the font, font size, font color, etc.
TabWidget vTabs = getTabWidget();
RelativeLayout rLayout = (RelativeLayout) vTabs.getChildAt(tabIndex);
((TextView) rLayout.getChildAt(textIndex)).setText("NewTabText");
or in one line...
((TextView)((RelativeLayout)getTabWidget().getChildAt(tabIndex)).getChildAt(textIndex)).setText("NewTabText");
where "textIndex" is the index of the text field. In this case it is 1. If the tab has an icon or custom modifications the index could change.
Thanks again to radek-k. You definitely got me pointed in the right direction.
You can do it without knowing the magic index of the TextView by using findViewById:
TabWidget vTabs = getTabWidget();
View indicatorView = vTabs.getChildAt(tabIndex);
((TextView) indicatorView.findViewById(android.R.id.title)).setText("NewTabText");
Here is your layout:
<com.google.android.material.tabs.TabLayout
android:id="#+id/ref_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_chpter"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_verse"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.google.android.material.tabs.TabLayout>
Here is your code:
TabLayout tabLayout = findViewById(R.id.ref_tabs);
tabLayout.getTabAt(0).setText("BOOK"));
tabLayout.getTabAt(1).setText("CHAPTER"));
tabLayout.getTabAt(2).setText("VERSE"));
TabWidget vTabs = .....;
// get desired tab view
View vTab = vTabs.getChildAt(i);
// I guess vTab is instance of TextView
TextView vText = (TextView) vTab;
vText.setText(...);
You can do it like this
TabHost tabHost = getTabHost();
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
tv.setText("New Tab Text");
Where android.R.id.title is System generated, just change ChildIndex and Text according to your need
This is simple and easy. Try it. It works.
TabWidget tab = (TabWidget) findViewById (R.id.tab1);
((TextView)tab.getChildTabViewAt (1)).setText ("New name here");
Related
I am using MaterialTabHost for displaying tabs. I wanted to change the font of the MaterialTabHost when onTabSelected. I tried to get the textview of the same. but cant figured it out. I referred https://github.com/neokree/MaterialTabs.
any help would be appriciated.
Here is my XML .
<!-- for Text Tabs -->
<it.neokree.materialtabs.MaterialTabHost
android:id="#+id/materialTabHost_MainActivity"
android:layout_width="match_parent"
android:layout_height="48dp"
android:animateLayoutChanges="false"
app:accentColor="#color/white"
app:primaryColor="#color/main_blue_light"
app:textColor="#color/white"
/>
so how do we change the font ?
This is how I change font of text into tab when it is create:
Typeface fontRobotoRegular = Typeface.createFromAsset(getAssets(),"font/RobotoCondensed-Regular.ttf");
MaterialTab tab = new MaterialTab(getApplicationContext(), false);
View tabView = tab.getView();
TextView text = (TextView) tabView.findViewById(R.id.text);
tab.setText(pagerAdapter.getPageTitle(i));
text.setTypeface(fontRobotoRegular);
tab.setTabListener(this);
tabHost.addTab(tab);
So if you want to change font when the tab is selected you can do something like this on onPageSelected of your ViewPager:
public void onPageSelected(int position) {
...
Typeface fontBold = Typeface.createFromAsset(getAssets(), "font/RobotoCondensed-Bold.ttf");
TextView text = (TextView) tabs.getCurrentTab().getView().findViewById(R.id.text);
text.setTypeface(fontBold);
...
}
Okay, so I have this code:
<?xml version="1.0" encoding="utf-8?">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/Text"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:text="Text"/>
</LinearLayout>
(Main Activity) below:
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement!");
This doesn't replace the text, and I do have the class, and main methods. onCreate etc.
It seems that the XML file is the only one it will take a value from, if there's no android:text it shows nothing.
I'm basically trying to just change the text via the .setText function, what am I doing wrong?
Thanks
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement");
}
}
Layouts for files: /storage/emulated/0/AppProjects/New/res/layout/main.xml
/storage/emulated/0/AppProjects/New/gen/com/mycompany/New/R.java
/storage/emulated/0/AppProjects/New/src/com/mycompany/new/MainActivity.java
Move your findViewById after setContentView(), and change your variable names to follow Java convention:
public class MainActivity extends Activity {
#Override
public void onCreate(savedInstanceState){
super.onCreate(Bundle savedInstanceState);
setContentView(R.layout.main);
//this line should be called after you inflate your view with setContentView
//so move it here, instead in MainActivity body
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement");
}
}
Also, there's a typo in your xml layout. Change this line:
<?xml version="1.0" encoding="utf-8?">
to
<?xml version="1.0" encoding="utf-8"?>
You cannot find your views before setContentView() method. Move your lines after setContentView():
TextView Text = (TextView) findViewById(R.id.Text);
Text.setText("Text Replacement);
Also, please follow java naming conventions, as obect names never start with Caps but class names do..
Move the below to onCreate
TextView Text = (TextView) findViewById(R.id.Text);
Like
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.Text);
tv.setText("Text Replacement);
findViewById looks for a view with the id in the current inflated layout. So first you need to set the layout to the activity and then initialize views.
Also consider renaming Text to something more appropriate and meaningfull.
Edit:
Make sure you have the textview with id Text in main.xml. Build and clean your project.
You should innialize your view and set proper properties in andriod after setContentView method is been called.
So this below code should come after setcontent view
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement);
Looks like you have two "" before the "Text"
Try deleting one...
Should look like this:
android:text="Text"/>
First, consider using "wrap_content" or "match_parent" attributes instead of "200dp" for example.
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Text"/>
Then, please rename your "Text" variable into "text". Java or Android may be thinking that Text is not your variable but a class:
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement!");
Ur variable is declared as TextView Text
Instead u should use text because in Java Anything starting with capital letter is treated as a class ,it is a bad programming practice ! The variables should be in lowercase(hungerian notation) !
Once I click the button, I want text of different sizes to be displayed. I tried using the built-in html support for it but I found out that font size tag is not supporI'm using a TextView to do this but I keep getting an error saying "could not execute method of the activity".
Here is my activity main part:
<TextView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32px" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rams"
android:onClick="ramsShow" />
I have the header string resource in string.xml:
<string name="header">Header</string>
This is the method in my MainActivity that is called when the button is clicked:
public void ramsShow(View view)
{
TextView test= new TextView(this);
test = (TextView) findViewById(R.id.header);
String tmp = "hi";
test.setText(tmp);
}
As Ketan suggested you should have:
public void ramsShow(View view)
{
TextView test = (TextView) findViewById(R.id.header);
String tmp = "hi";
test.setText(tmp);
}
To adjust sizes have a look here: How to set text size of textview dynamically for different screens
In Activity class, above the onCreate Method, add this line.
TextView test = null;
Within onCreate method, add this code
test = (TextView) findViewById(R.id.header);
Idea is to move the line from ramsShow method to onCreate method.
Within onCreate() write this code...
RelativeLayout lView = new RelativeLayout(this);
myText = new TextView(this);
myText.setText(Html.fromHtml("<b><center><font size='3'><font color=\"#145A14\">Welcome standard\n"</font></font></center></b>"));
lView.addView(myText);
setContentView(lView);
}
I have this LinearLayout that is a child of a RelativeLayout along with a ListView among other things:
<LinearLayout
android:id="#+id/color_bar"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="16dp"
android:padding="0dp"
android:orientation="horizontal"
>
<TextView
android:id="#+id/space_used_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#006688"
android:padding="0dp"
/>
<TextView
android:id="#+id/space_free_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#444444"
android:padding="0dp"
/>
</LinearLayout>
I don't intend to put any text in those TextViews; they are simply there for their background color values. I want to set the width's of these two TextViews programmatically, which I can do, but the problem is that the first time the LinearLayout is presented, it is not drawn. It has no size and I also cannot see the TextViews contained within it. When the user does almost anything (e.g. lock the screen, press the home button, click a list item, select an options item, etc.) the TextViews display properly. It's just that at the first moment when the activity opens, the TextViews and the Layout doesn't show up at all. Does anyone have any idea what the problem might be?
P.S. I have already tried calling invalidate on the LinearLayout as well as the individual TextViews.
EDIT: Here are the callbacks
#Override
public void onCreate(Bundle savedInstanceState)
{
//Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.browser);
topMenu = getActionBar();
lv = (ListView) findViewById(R.id.file_list);
spaceUsedBar = (TextView) findViewById(R.id.space_used_bar);
spaceFreeBar = (TextView) findViewById(R.id.space_free_bar);
spaceUsed = (TextView) findViewById(R.id.space_used);
spaceFree = (TextView) findViewById(R.id.space_free);
colorBar = (LinearLayout) findViewById(R.id.color_bar);
stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
if (savedInstanceState == null)
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
currentDirectory = externalStorageDirectory;
else
{
currentDirectory = new File(ROOT_DIR);
Toast t = Toast.makeText(c, R.string.not_mounted, Toast.LENGTH_SHORT);
t.show();
}
}
else
{
currentDirectory = new File(savedInstanceState.getString("savedPath"));
int savedPosition = savedInstanceState.getInt("savedPosition");
int savedListTop = savedInstanceState.getInt("savedListTop");
if (savedPosition >= 0)
lv.setSelectionFromTop(savedPosition, savedListTop);
}
}
#Override
public void onStart()
{
//Log.d(TAG, "onStart()");
super.onStart();
lv.setOnItemClickListener(this);
lv.setMultiChoiceModeListener(this);
browseTo(currentDirectory);
}
#Override
public void onResume()
{
//Log.d(TAG, "onResume()");
super.onResume();
}
I guess that you haven't redrawn the layout after setting a new width for the TextViews and when the system redraws the layout after the user leaves then returns (locking the screen, home button, orientation change, etc). But I don't see your onCreate() and onResume() code, so it is only a guess...
I'm not sure if this will work but try one of the following (on the textviews). For instance you assign it some initial width or weight, and then adjust it accordingly programmatically when that code executes...
android:layout_width="40dp"
If you want them to take up a percent of the screen instead use the weight attribute:
android:layout_weight="2"
This seems like it should be simple, but I can't figure out a way to do it. I'm needing a tab to have beginning text, but then have that text change after the user selects an item from a list. I know how to change tab backgrounds and colors via
mTabHost.getChildAt(index).setBackgroundColor();
but there isn't an option to change the tab's indicator. I've tried using an EditText.
private EditText tabName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statistics);
comm = new Communicator();
tabName = new EditText(this);
tabName.setText("BeginningText");
mTabHost = getTabHost();
mTabHost.addTab(mTabHost
.newTabSpec("tab_1_stat")
.setIndicator(User)
.setContent(R.id.meStatsTab));
mTabHost.addTab(mTabHost
.newTabSpec("tab_2_stat")
.setIndicator(tabName.getText())
.setContent(R.id.themStatsTab));
mTabHost.addTab(mTabHost
.newTabSpec("tab_3_stat")
.setIndicator("Archive")
.setContent(R.id.archiveStatsTab));
mTabHost.setOnTabChangedListener(this);
getTabWidget().getChildAt(1).setOnClickListener(new onThemTabClicked());
mTabHost.setCurrentTab(0);
onTabChanged("tab_1_stat");
}
.....
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
tabName.setText("ChangedTabText");
Bundle extras = intent.getExtras();
themStats = extras.getStringArray("themStats");
mTabHost.setCurrentTab(1);
onTabChanged("tab_2_stat");
}
That didn't work either, along with a few other attempts. Any ideas? Thanks ahead of time!
try this easiest way which works for me :
tabLayout.getTabAt(index).setText("TabName");
Wow. Okay this was a pain. Apparently TabWidget does something funky with RelativeLayout and everytime I tried to do anything with radek-k's solution it was blowing up with a RelativeLayout error. So basically the work around is the following. It also allows you to change the font, font size, font color, etc.
TabWidget vTabs = getTabWidget();
RelativeLayout rLayout = (RelativeLayout) vTabs.getChildAt(tabIndex);
((TextView) rLayout.getChildAt(textIndex)).setText("NewTabText");
or in one line...
((TextView)((RelativeLayout)getTabWidget().getChildAt(tabIndex)).getChildAt(textIndex)).setText("NewTabText");
where "textIndex" is the index of the text field. In this case it is 1. If the tab has an icon or custom modifications the index could change.
Thanks again to radek-k. You definitely got me pointed in the right direction.
You can do it without knowing the magic index of the TextView by using findViewById:
TabWidget vTabs = getTabWidget();
View indicatorView = vTabs.getChildAt(tabIndex);
((TextView) indicatorView.findViewById(android.R.id.title)).setText("NewTabText");
Here is your layout:
<com.google.android.material.tabs.TabLayout
android:id="#+id/ref_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_chpter"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_verse"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.google.android.material.tabs.TabLayout>
Here is your code:
TabLayout tabLayout = findViewById(R.id.ref_tabs);
tabLayout.getTabAt(0).setText("BOOK"));
tabLayout.getTabAt(1).setText("CHAPTER"));
tabLayout.getTabAt(2).setText("VERSE"));
TabWidget vTabs = .....;
// get desired tab view
View vTab = vTabs.getChildAt(i);
// I guess vTab is instance of TextView
TextView vText = (TextView) vTab;
vText.setText(...);
You can do it like this
TabHost tabHost = getTabHost();
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
tv.setText("New Tab Text");
Where android.R.id.title is System generated, just change ChildIndex and Text according to your need
This is simple and easy. Try it. It works.
TabWidget tab = (TabWidget) findViewById (R.id.tab1);
((TextView)tab.getChildTabViewAt (1)).setText ("New name here");