How do I use androids default window title style to make my own similar TextView?
I have done a lot of guessing and made a TextView that has everything that
the default title bar has, except for the text shadow (and some padding/margins etc. I think).
Here's essentially what I've tried:
MainUI.xml
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
}
title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:textAppearance="#android:style/TextAppearance.WindowTitle"
android:background="#android:drawable/title_bar"
android:text="This is my new title" />
Edit:
I found some interesting related articles at makemachine and anddev.
Although I don't like it, I copied some attributes from the implementation style.xml.
Is there any way to avoid copying the attributes in this static way?
The following rendered almost perfect, the difference is in fact that the original did "cut" the first 2-3 pixels of the title shadow, while my TextView doesn't.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center_vertical"
android:shadowColor="#BB000000"
android:shadowRadius="2.75"
android:singleLine="true"
android:textAppearance="#android:style/TextAppearance.WindowTitle"
android:background="#android:drawable/title_bar"
android:text="This is my new title" />
It's also important to override the default android:windowTitleBackgroundStyle with a transparent color, because the default includes some padding etc. that you don't want to be wrapping your custom title bar.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#android:color/transparent</item>
</style>
</resources>
Remember to enable the theme in your AndroidManifest.xml
try this it worked for me:
put the rest of the code in place..
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.main_header);
TextView home = (TextView) findViewById(R.id.home);
if ( home != null ) {
/* your code here */
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// the action that u want to perform
Menu.this.finish();
Intent i= new Intent(Main.this,Destination.class);
startActivity(i);
}
});
}
create a main_header layout file
<?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" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Main Menu"
android:textStyle="bold"
android:textSize="10pt" />
<TextView
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentRight="true"
android:textStyle="bold"
android:text="Home"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
</LinearLayout>
here when we execute the code it will show a custom title bar where there are two text's in it and when we click on that it will go the next class ( to your destination class). we can also change the color , size font etc.. and also add buttons etc to the custom title bar.
Related
Steps to reproduce:
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"/>
MainActivity.java
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:textColorPrimary"
android:text="Hello World!"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:textColorSecondary"
android:text="Hello World!"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:textColorTertiary"
android:text="Hello World!"/>
</LinearLayout>
Start the MainActivity and we get:
Use the layout inspector we read the current text color of the lower two TextViews to be the same value -1979711488, which corresponds to color hex #8a000000. I looked up the source code of Android and found that textColorSecondary is controlled by an alpha value defined in colors_material.xml as:
<item name="secondary_content_alpha_material_light" format="float" type="dimen">0.54</item>
which translates to #8a0000000. And textColorTertiary is defined explicitly to be #8a0000000. However, in my Android Studio 3.0.1, design panel, they show as three different colors:
Is it true that both colors are defined the same? or what am I missing to properly use them?
I have a class that extends EditTextPreference and I use it in my own layout, such as:
<com.app.MyEditTextPreference
android:key="#string/key"
android:title="#string/title"
android:summary="#string/summary"
android:dialogTitle="#string/title"
android:dialogMessage="#string/message"
android:layout="#layout/preference_edittext"/>
My app theme inherits from Theme.AppCompat.Light.DarkActionBar and I have changed the values of the text sizes:
<style name="TextAppearance.Small">
<item name="android:textSize">18sp</item> <!-- was 14sp -->
</style>
However, when the preference dialog is displayed, the size of the message is different than the one I have defined in my style.
So how do I set the text size of the dialog message correctly?
EDIT
I copied the dialog layout from the sdk:
<LinearLayout
android:id="#+android:id/edittext_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:id="#android:id/message"
android:layout_marginBottom="48dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:textColor="?android:attr/textColorSecondary"
style="#style/TextAppearance.Small" />
</LinearLayout>
However, I get a build error Resource is not public on the line android:id="#+android:id/edittext_container".
And if I change it to "#*android:id/edittext_container", the edit text is not visible anymore.
Is there a simpler way to just change the textsize of the message?
First create new xml file name cus.xml in layout folder.
it need for set the view for dialog at runtime .
<?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">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Name :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18dp" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:textSize="18dp" />
As you need dialog:
here's dialog code:
Dialog d=new Dialog(getActivity());
d.setTitle("Image Title");
d.setContentView(R.layout.cus);
d.show();
I finally managed to change the message text size by:
a) Changing the id of the LinearLayout to: #+id/edittext_container, and
b) Adding the following code in my class extending EditTextPreference:
#Override
protected void onAddEditTextToDialogView(View dialogView, EditText editText) {
ViewGroup container = (ViewGroup) dialogView
.findViewById(R.id.edittext_container);
if (container != null) {
container.addView(editText, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
This works fine and is clearly a valid answer, however a simpler solution would be to just change the message text size through themeing.
I want to add a custom action title on the left of the actin bar, replacing with the default title just like in the below image the default image is shown
And here I want to add this title.
You need to change the logo and the title in action bar.
You can use
getActivity().getActionBar().setTitle("your title");
and
getActivity().getActionBar().setLogo(your draw id);
It is very easy to do ..
There's lot's of way to do this task as defined above .. but I will suggest to make use of custom view for Action bar(As this will work on all devices as same) .. this will let you create much more complex view's in a very handy and easy way.
For your query you make make use of custom views for ActionBar..
You can create customized action bar like this ..
actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.xyz); // Set custom view like this
// In your case you want an icon ...
ImageView image = (ImageView) actionBar.getCustomView().findViewById(R.id.fare_ll); // Get id of custom view like this
Also you need to set this property ..
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
// Now you can do any sort of stuffs with this icon .. like you can perform click functionality
Here's your xyz.xml file .. This file is just for your reference purpose .. this will let you know how to create custom view for Action Bar. :)
<?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="fill_parent"
android:orientation="horizontal"
>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center|start"
android:id="#+id/confirm_cabs_back"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/b"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/city_spinner_layout"
android:layout_gravity="center"
>
<TextView
android:id="#+id/action_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XYZ"
android:textStyle="bold"
android:textColor="#color/taxi_blue"
android:layout_gravity="center"
/>
<!-- <ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/drop_down" /> -->
</LinearLayout>
</LinearLayout>
<!-- <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center"
android:gravity="end"
android:id="#+id/fare_ll"
> -->
<LinearLayout
android:layout_width="2dp"
android:layout_height="fill_parent"
android:background="#color/grey" >
</LinearLayout>
<!-- android:layout_marginTop="2dp"
android:layout_marginBottom="2dp" -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hard_61"
android:id="#+id/fare_ll"
android:layout_gravity="center|end"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:textColor="#color/black"
/>
<!-- </LinearLayout> -->
</LinearLayout>
You are good to go ..
You need to use the following code to get this:
private ActionBar actionBar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
actionBar.setTitle("");
actionBar.setLogo(R.drawable.logo); //logo needs to be defined in the drawables folder.
}
I have a problem about the title bar. I want to do 2 buttons in the title bar, but it is seen really bad. First I was thinking I got the button height too big, but when I change the button height nothing is changed. I don't want a custom titlebar and I can't see the text in the button. Can anybody help me?
My code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/titleLayout">
<Button android:id="#+id/done"
android:layout_width="60dip"
android:layout_height="38dip"
android:text="done"
android:layout_toRightOf="#+id/edit"
>
</Button>
<Button android:id="#+id/edit"
android:layout_width="60dip"
android:layout_height="38dip"
android:text="edit"
>
</Button>
</RelativeLayout>
code nippet:
final boolean customTitle = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
if ( customTitle ) { Window win = getWindow();
win.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title);
Jsut try to make your RelativeLayout's height WRAP_CONTENT
You can do like this. Before initializing setContentView(), write this line of code
requestWindowFeature(Window.FEATURE_NO_TITLE);
Then in your layout XML file, include your custom title bar like this.
<include layout="#layout/title" android:id="#+id/title"/>
What about just using Text and formatting the background, edges, etc. to make it look like a button. I would try using a TextView, wrap_content and then play with paddings, and background.
<RelativeLayout xmlns:android=" http://schemas.android.com/apk/res/android "
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/titleLayout">
<TextView
android:id="#+id/doneBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="#drawable/bluebutton"
android:text="Done"
android:textStyle="bold"
android:textColor="#FFFFFFFF"
/>
</RelativeLayout>
and then in your #drawable/bluebutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=" http://schemas.android.com/apk/res/android ">
<item>
<shape>
<gradient
android:startColor="#FFBBDDFF"
android:centerColor="#FF191990"
android:endColor="#FF0000FF"
android:angle="270" />
</shape>
</item>
</selector>
Look at this link for more details I hope it helps...
Standard Android Button with a different color
I have my main.xml up and in the XML code I have:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<ImageView android:id="#+id/imageView1" android:layout_height="wrap_content" android:src="#drawable/logo" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="116dp"></ImageView>
<TextView android:layout_height="wrap_content" android:text="Administration 0.0.1" android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_below="#+id/imageView1" android:layout_centerHorizontal="true"></TextView>
</RelativeLayout>
But when I run the app in an emulator, the default theme still shows with the black background. I can't figure out why it's doing this.
Any ideas?
What background color are you expecting? It shows up correctly for me if you're expecting white. This is what the screen looks like (I have substituted your image with the android icon)
Does your onCreate() look like this -
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}