Change title bar text in Android doesn't work - java

I'm trying to customize the title bar in my android application. In order to do that, I used accepted answer of this question.
Here is the code I used.
Here is my titlebar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:id="#+id/ImageView01"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_prof"/>
<TextView
android:id="#+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#FFA500"
/>
</LinearLayout>
Here is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Here is my TitleBar.java
package com.myayubo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class TitleBar extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if (myTitleText != null) {
myTitleText.setText("NEW TITLE");
// user can also set color using "Color" and then
// "Color value constant"
myTitleText.setBackgroundColor(Color.GREEN);
}
}
}
Here is my strings.xml
<string name="hello_world">Hello world!</string>
<string name="app_name">My Ayubo</string>
<color name="titlebackgroundcolor">#3232CD</color>
<color name="titletextcolor">#FFFF00</color>
But, I cannot customize my title bar using following code. I mean, code is running without any errors. But, it is not doing what I expected. What I need is to add an image and a title in to my header. How am I suppose to do that?

figured it out in my own.
First added following items in my menu.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="1"
android:showAsAction="never" />
<item
android:id="#+id/action_example"
android:title="Logout"
android:orderInCategory="2"
android:showAsAction="withText|ifRoom" />
<item
android:id="#+id/profPicture"
android:title=""
android:orderInCategory="3"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_prof"
android:onClick="userProfile"
/>
<item
android:id="#+id/dealsPic"
android:title=""
android:orderInCategory="2"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_deals"
android:onClick="doThis"/>
<item
android:id="#+id/addLoc"
android:title=""
android:orderInCategory="1"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_location"
android:onClick="gotoLocation"/>
<item
android:id="#+id/namePerson"
android:title="Nathalia Smith"
android:orderInCategory="4"
app:showAsAction="ifRoom"
android:onClick="userProfile"/>
</menu>
Then added following methods to java file to implement onlick events of images and buttons.
public void doThis(MenuItem item){
// Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), DealsList.class);
finish();
startActivity(intent);
}
public void gotoLocation(MenuItem item){
// Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), AddLocation.class);
finish();
startActivity(intent);
}
public void userProfile(MenuItem item){
// Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), UserProfile.class);
finish();
startActivity(intent);
}
Thanks for everyone who answered. :)

Related

Android custom dialog button styles not applied

I wish to create a simple dialog for the user with 2 buttons as follows:
Dialog Layout (dialog_layout.xml):
<?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:layout_margin="8dp"
android:orientation="vertical">
<Button
android:id="#+id/btn_select_choice_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Choice"
android:theme="#style/secondary_button_normal" />
<Button
android:id="#+id/btn_select_choice_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Choice"
android:theme="#style/secondary_button_normal" />
</LinearLayout>
secondary_button_normal:
<style name="secondary_button_normal" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/button_secondary_normal_background</item>
<item name="android:textColor">#color/button_secondary_normal_text</item>
<item name="android:textSize">#dimen/button_textSize</item>
<item name="android:padding">#dimen/button_padding</item>
</style>
Activity's onCreate:
final Dialog selection = new Dialog(this);
selection.setContentView(R.layout.dialog_layout);
Button selectFirstChoice = (Button)selection.findViewById(R.id.btn_select_choice_1);
Button selectSecondChoice = (Button)selection.findViewById(R.id.btn_select_choice_2);
selectFirstChoice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
selection.dismiss();
}
});
selectSecondChoice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
selection.dismiss();
}
});
selection.setTitle("Some Title");
selection.setCancelable(false);
selection.show();
The preview is alright:
Preview
It works well on Nougat, but when I run it on Lollipop (5.0 and 5.1.1), the buttons are without styling, although the same button styling worked on activity buttons on Lollipop:
App
I wonder what could be going wrong, I also tried moving the Dialog into a DialogFragment but I faced the same behavior.
Found the following solution:
In my Styles.xml, I added:
<style name="dialog_button" parent="Theme.AppCompat.Light.Dialog">
<item name="colorButtonNormal">#color/button_secondary_normal_background</item>
<item name="android:textColor">#color/button_secondary_normal_text</item>
<item name="android:textSize">#dimen/button_textSize</item>
<item name="android:padding">#dimen/button_padding</item>
</style>
And in my custom dialog layout, I used that style as a theme for my buttons:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_margin="8dp"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_select_student"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/student"
android:theme="#style/dialog_button" />
<Button
android:id="#+id/btn_select_tutor"
android:layout_width="match_parent"
android:text="#string/tutor"
android:layout_height="wrap_content"
android:theme="#style/dialog_button" />
</LinearLayout>

Toolbar icons not appearing in AppCompat Activity

I am trying to add toolbar to my app but somehow it is not working. Neither title not action icons are appearing. Even " : " menu is also not appearing. Here are my codes:
Style.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.vaibhav.thirdeye.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item android:id="#+id/next"
android:title="#string/next"
android:orderInCategory="200"
android:icon="#drawable/next_arrow"
app:showAsAction="always"/>
</menu>
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/back_splash"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.vaibhav.thirdeye.MainActivity"
tools:showIn="#layout/activity_main"
app:theme="#style/AppTheme.NoActionBar">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
MainActivity.java:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.vaibhav.thirdeye.MESSAGE";
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Adding Custom toolbar
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.next_arrow);
toolbar.setTitle("ThirdEye");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_settings:
Toast settings = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
settings.show();
case R.id.next:
Toast next = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
next.show();
}
return true;
}
}
app_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/icon_size"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
Output Screenshot:
Any helps????
Remove extra toolbars in your MainActivity's activity_main.xml layout, it will solve your problems.

Button in dialog not behaving as a button when clicked

After create and testing my custom dialog I've noticed that my button does not show any visual sign of change (such as the ripple effect or highlighting) when clicked. Does anyone know what I'm doing wrong and how to resolve this issue?
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<Button
android:id="#+id/world"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:background="#color/green"
android:textColor="#color/white"
android:text="#string/world"
android:textAllCaps="false"
style="#android:style/TextAppearance.Medium"/>
</LinearLayout>
Java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_other_lines) {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_hello_world);
dialog.setTitle("Dialog");
Button world = (Button) dialog.findViewById(R.id.world);
world.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
return super.onOptionsItemSelected(item);
}
You override the default onClick effect by putting a background color.
android:background="#color/green"
You can do it by creating a custom background xml file on drawable, like this.
custom_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/Pressed_Color" />
<item android:state_activated="false" android:drawable="#color/green"/>
</selector>
then you call it on the button styling as like this:
android:background="#drawable/custom_background"
when you change your default appearance, you should handle different state if you want like this
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_sel" android:state_selected="true" />
<item android:drawable="#drawable/button_sel" android:state_pressed="true" />
<item android:drawable="#drawable/button_unsel" />
</selector>
save this as xml drawable and add as android:background

Android setting custom actionbar

In my project i would to set a custom actionBar (with a custom layout) after a specific action.
I have this simple Activity:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity2 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//apriImpostazioni ();
return true;
case R.id.information_item:
//apriInformazioni ();
return true;
case R.id.search_item:
apriBarraRicerca ();
System.out.println ("IL BOTTONE RICERCA E' PREMUTO");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void apriBarraRicerca () {
getActionBar ().setCustomView(getLayoutInflater().inflate(R.layout.search_layout, null));
}
}
Where "menu_main_activity2" is this xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="zipangulu.myapplication.MainActivity2">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/information_item"
android:title="Info"
android:icon="#drawable/info"
android:orderInCategory="100"
app:showAsAction="always"/>
<item android:id="#+id/search_item"
android:title="Search"
android:icon="#drawable/search_icon"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
I would, pressing the search_item on the main actionbar, set a custom actionBar whith this layout:
<?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="wrap_content"
android:background="#4584d3">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/back_button"
android:background="#null"
android:src="#drawable/back_icon"/>
<AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:id="#+id/campo_ricerca"/>
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:spinnerMode="dropdown"
android:id="#+id/spinner_anno"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/search_icon"
android:id="#+id/avvia_ricerca"/>
</LinearLayout>
But at runtime I have a NullPointerException in the body of "apriBarraRicerca" method..why?How can I fix this problem?
EDIT: as suggested I replaced "getActionBar ()" with "getSupportActionBar ()", now I don't have any exception but nothing happens.
EDIT2: I added getSupportActionBar().setDisplayShowCustomEnabled(true); and now my custom actionbar is visible but not as i want, look at the following image:
http://bit.ly/1Dc2kGg
The bar is visible but cutted, and are visible also the items of the previous actionBar..
You can try do the same thing with the android.support.v7.widget.ToolBar
Your Layout which you can populate as you desire:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#688A08"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
You have to modify the values->styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
And the in the onCreate() method you do the following:
Toolbar tb = (Toolbar)findViewById(R.id.top_bar);
if(tb != null){
setSupportActionBar(tb);
}
Hope it helps!!!

Button topmost in android

I have layer, it is transparent. I can click on button only, I want to click through layer.
Here's my Manifest:
<activity android:name=".Test" android:theme="#style/Theme.Transparent"/>
Here's Transparent theme style:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Here's my Activity code:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
// v I can touch through app, but cant click on button
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
// ^ I can touch through app, but cant click on button
Button xclose = (Button) findViewById(R.id.buttonx);
xclose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
}
}
Here's my test.xml layout:
<?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" >
<Button android:id="#+id/buttonx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="X" />
</RelativeLayout>
Try this code and set click listener on the linear layout
<?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" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll">
<Button android:id="#+id/buttonx" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" android:text="X" />
</LinearLayout>
</RelativeLayout>
You already do the following here to set the click listener for the X button.
Button xclose = (Button) findViewById(R.id.buttonx);
xclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
Now all you need to do is the same thing but for the Layout which you have created that is transparent. So that when you click the layout in the area that your play button is, it listens to the click, rather than the button, as the button is out of reach.

Categories