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.
Related
I have a TextView in my .xml file and below that TextView I have a button. The text in TextView changes everytime when the app is running. I created a custom dialog so that when I press the button the custom dialog shows. In this custom dialog I also have a TextView and I want this TextView to take and dislplay data that is in my main TextView.
Here is my main.xml file
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/country"
android:layout_marginTop="10dp"
android:text="country"/>
And this is TextView in custom_dialog.xml file
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/get_country"
android:text=""/>
Below is my MainActivity.class custom dialog in onCreate()
customDialog= new Dialog( this );
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.setContentView(R.layout.custom_dialog);
customDialog.setCancelable( true );
TextView textContry=(TextView)customDialog.findViewById(R.id.get_country);
I need this TextView in custom_dialog to take data in TextView of main.xml when the dialog shows.
You have to set text in custom dialog textView by taking the text from main text view on a button click.
textContry.setText(textviewMain.getText().toString());
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.
I am trying to add an editText in between two already existing editTexts programmatically on the click of a button in android. I am just wondering if this is possible, as i have been unable to find any related questions?
What do you want to acomplish with runtime element adding? Whats the purpose? Maybe isn't enough to show/hide the element on specific actions?
I mean you can make it gone (it will be invisible but also won't use space on the layout) in xml:
android:visibility="gone"
or in java code in the onCreate() method:
specificElement.setVisibility(View.GONE)
Then when you normally would add the element you rather just set the visibility to visible:
specificElement.setVisibility(View.VISIBLE)
What about that?
you can add 3rd EditText on 2nd position.
For that first you should have reference of the parent layout nad then do like this.
if you have done :
parent.addView(editText1);
parent.addView(editText2);
So now your parent have two child views.
now to add 3rd EditText i.e. editText3 then do this like:
parent.addView(editText3, 1);// addView(<childview>, <index>);
Like this your 3rd EditText will be in 2nd position.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)yourEditBox.getLayoutParams();
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
params.addRule(RelativeLayout.RIGHT_OF, R.id.id_to_be_right_of);
yourEditBox.setLayoutParams(params);
Use above code to add and align you're editbox between another two editboxes.
Inside Activity class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(linearLayout);
EditText editText = new EditText(getBaseContext());
editText.setHint("Programmatically Added EditText");
linearLayout.addView(editText, 1);
}}
Layout file structure
<?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"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 2" />
</LinearLayout>
Hope this helps. Cheers!
You can just add this editText between the two in your xml file and controle visibility on your xml and the moment you click on your button.
In xml file :Set visibility to gone or invisible depending on what you actually want :
android:visibility="invisible" it won't be visible but it's going to take place in your view
android:visibility="gone" it won't be visible and it's not taking place in your view
In your code :
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
yourButton.setVisibility(View.VISIBLE);
});
This seems to be such a fundamental question that I'm embarrassed to ask it, but I'm so frustrated by my Fragment learning curve that I'll expose my ignorance.
An example in a textbook that cuts a lot of corners that make expanding them difficult, if they even work, had no button to click; MainActivity simply loaded FragmentA. OK, keep it basic. I get that.
So I added a button to MainActivity to click to load FragmentA, but the button shows on the FragmentA screen, sort of like this (not an actual screen shot, but close):
How do I prevent that? Should I use a second Activity instead of a Fragment? Since this endeavor is to be utilized in a much larger project, I don't want to do anything considered not best practice. I realize that the main use of Fragment is to enable side-by-side "screens" on devices that are large enough. That's not what I want to do, but it IS possible to accomplish what I want with a Fragment, isn't it?
MainActivity.java
public class MainActivity extends Activity {
#Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate( savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnLoadFragmentAByClick(View view)
{
FragmentA fragmentA;
fragmentA = new FragmentA();
FragmentTransaction ft ;
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.layout_container, fragmentA);
ft.addToBackStack("example");
ft.commit();
}
}
FragmentA.java
public class FragmentA extends Fragment
{
#Override
public View onCreateView(LayoutInflater _inflater,
ViewGroup _container,
Bundle _savedInstanceState)
{
return _inflater.inflate(R.layout.fragment_a,
_container,
false);
}
}
activity_main.xml
<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" >
<LinearLayout
android:id ="#+id/layout_container"
android:orientation ="vertical"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
<Button
android:id ="#+id/btnLoadFragmentA"
android:text ="Load Fragment A"
android:onClick="btnLoadFragmentAByClick"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
fragment_a.xml
<RelativeLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height ="match_parent" >
<TextView
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="Layout for fragment A"
android:textAppearance ="?android:attr/textAppearanceLarge"
>
</TextView>
</RelativeLayout>
EDIT
I realize that I could hide the MainActiviy button (and any other objects) before loading FragmentA and show them after returning, but I was hoping for a one-or-two-line "fix".
How do I prevent that?
Well, to some extent, you don't, insofar as this has nothing to do with fragments.
Your activity_main.xml has the Button floating over top of the LinearLayout (???) that you are using for your fragment container. If you do not want the Button floating over top of the fragment container, then fix the layout file to not have the Button floating over top of the fragment container.
I realize that I could hide the MainActiviy button (and any other objects) before loading FragmentA and show them after returning, but I was hoping for a one-or-two-line "fix".
The typical solution for full-UI replacement using fragments is to have everything in fragments. Your replace() would replace your original fragment with a replacement. So, in this case, your Button would be managed by one fragment, and clicking the Button would replace() that fragment with another fragment. Given that your FragmentTransaction has addToBackStack(), pressing BACK would get rid of the replacement fragment and return you to your Button fragment.
I am trying to put a button in the action bar instead of the application icon which means at left. I have tried orderInCategory but it doesn't help.
What is the right way to do that?
If you just want to change the icon of the "home" section of the Action Bar, you can use ActionBar.setIcon() programatically, or set android:icon in your XML. If you want to do something more complex than that (have multiple buttons, or be able to customize them further), you need to set a custom view in your action bar.
To set a custom view in your action bar, you need to do the following:
Create a layout for your custom view:
<LinearLayout
android:width="wrap_content"
android:height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/button1"
android:width="wrap_content"
android:height="wrap_content"
android:src="#drawable/button1" />
</LinearLayout>
And then inflate it and set it as your action bar's custom view:
LayoutInflater inflater = LayoutInflater.from(context);
View customView = inflater.inflate(R.layout.custom_view, null);
ImageView button = (ImageView) customView.findViewById(R.id.button1);
button.setOnClickListener(...);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(customView);
Hope that helps