how to customize titlebar with an image in android app? - android

I would customize the title bar of every activity of my application with an icon and a text. I've tried some ways but they doesn't work. can anybody help me?
this is an example of my code:
public class TitleBar extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
// user can also set color using “Color” and then “Color value constant”
// myTitleText.setBackgroundColor(Color.GREEN);
}
}
in 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:orientation="vertical" >
<TextView
android:id="#+id/myTitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="appuntiiiiiii" />
</LinearLayout>`

I know it might be overkill for your needs but if you really want something cool you should try implementing ActionBar. It's avalible officialy from API11 but you can implement ActionBarSherlock which looks and works pretty much the same. I'm loving it!
As for your code, there is no ImageView so how could you possible display an image. You need an imageView before your TextView

Related

fitsSystemWindows="true" doesn't work after calling setContentView()

For an app I'm developing I wanted to reload the UI after a user input (basically resetting it completely after they made changes to it). I wanted to try avoiding destroying/recreating the activity and use setContentView() instead because it's a lot faster.
However when I do that I'm having an issue: the newly created UI doesn't respect the fitsSystemWindows="true" and part of it ends up behind the android's status bar.
I managed to boil it down to that code example to test it :
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="#+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button" />
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
reloadUI();
}
});
}
public void reloadUI() {
setContentView(R.layout.layout);
}
}
When I load the app, I get the expected layout, that is a simple button on top of the screen, right below the status bar:
However, once I click the button which calls setContentView a second time (showing the same XML), the Button gets behind the status bar:
Calling mainContainer.getMeasuredHeight() to check what happens gives me 1848px on the first start of the app (on a screen that is 1920px tall, so its height is 72px less than the whole screen, 72px being the height of the status bar), but once I call setContentView again the mainContainer.getMeasuredHeight() gives me 1920px.
Am I missing something here? I could force the mainContainer to stick to a 1848px height with a 72px top padding, but I'd prefer to avoid an ugly hack like this.
So, what you want is to ask the framework to dispatch once more WindowInsets to your root view. That's precisely what ViewCompat.requestApplyInsets(View) will do:
Ask that a new dispatch of View.onApplyWindowInsets(WindowInsets) be performed. This falls back to View.requestFitSystemWindows() where available.
Applying just one line should resolve all your concerns:
public void reloadUI() {
setContentView(R.layout.layout);
// `R.id.mainContainer` is the id of the root view in `R.layout.layout`
ViewCompat.requestApplyInsets(findViewById(R.id.mainContainer));
}
I had the same problem. My solution is to change the rootView marginTop or paddingTop to adapt to the View manually.

PreferenceFragment faults on id.list but not on id.content

What I really want to do is add a preference screen to an existing layout with other items in it (such as a button) without using deprecated approaches. I have looked at the 'add button to preference screen' and I get that to semi-work using deprecated approaches.
I have two xml layouts. First the preference screen (at the moment the PreferenceCategory is superfluous) with 12 other CheckBoxPreferences omitted for clarity:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="#string/specialization_prefs"
android:title="#string/select_specializations">
<CheckBoxPreference
android:key="#string/pulse_ox_key"
android:title="#string/pulse_ox"
android:defaultValue="true" />
<CheckBoxPreference
android:key="#string/bp_key"
android:title="#string/bp"
android:defaultValue="true" />
</PreferenceCategory>
Then I have the activity layout (PreferenceActivity or Activity; does not seem to matter):
<?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"
android:id="#+id/specializations_layout" >
<Button
android:id="#+id/btn_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Exit and Save" />
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
The ListView was meant to be a dummy to be replaced with the PreferenceFragment which I got from a Stackoverflow suggestion. The Button I want to keep.
My PreferenceFragment is as follows:
public class SpecializationsFragment extends PreferenceFragment
{
#Override
public void onAttach(Activity specializationsActivity)
{
super.onAttach(specializationsActivity);
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.specializations);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return super.onCreateView(inflater, container, savedInstanceState);
}
}
Finally my PreferenceActivity is as follows:
public class SpecializationsActivity extends PreferenceActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.specializations_layout);
getFragmentManager().beginTransaction()
.replace(android.R.id.list, new SpecializationsFragment())
.commit();
}
}
If I replace the 'android.R.id.list' with 'android.R.id.content' it sort of works. I see my button but my preference screen scrolls right over it. This is what I get when I follow the deprecated button solution. If I use the 'android.R.id.list' instead, the application crashes saying "addView(View) is not supported in AdapterView.
I have searched on that problem as well. What am I missing? This code currently does nothing but a GUI display. What do I need to add to get the button to display and the preference screen to scroll in the space below it as it is supposed to do in its own 'fragment'? I thought that was the whole idea behind fragments. (Yes they actually work quite well as long as preference screens are not present.) It also makes no difference using the 'add' method versus the 'replace' method.
For anyone who cares I found a solution. Not what I expected but it worked. First I needed to nest a layout within my layout. Then I needed to align this nested layout below the button. I also needed to have the dummy ListView in there but I do not know why. So the xml appears as follows:
<?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/btn_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Exit and Save" />
<RelativeLayout
android:layout_below="#+id/btn_done"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/specializations_layout" >
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
</RelativeLayout>
The code now has the replace method (using 'add' or 'replace' made no difference) using the id added to the nested relative layout as follows:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.specializations_layout);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.specializations_layout, new SpecializationsFragment())
.commit();
findViewById(android.R.id.list).setBackgroundColor(Color.argb(0, 0, 0, 0));
}
I don't understand what is going on under the hood here, especially with the need for the dummy element and its android:id/list attribute. I would like to have seen it work without the ListView element as I believe would be the case if this was an ordinary fragment and not a preference screen. I would be grateful to anyone who can explain this to me!

setProgressBarIndeterminateVisibility(true) not working

I'm trying to add a progress bar to my actionBar. I'm talking about the spinning circle. I did a request and tried to set is vissible, but nothing happens.
I have read many likely questions but is still couldn't figure out what i'm doing wrong.
My code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//Above setContentView, very important
setContentView(R.layout.activity_main);
//...other stuff
}
In an other method which i don't call in on create.(It's an onClick method)
public void plus(View view){
setProgressBarIndeterminateVisibility(true);//Nothing happens
//...other stuff
}
I do not understand what's wrong, please help me.
NOTE: I never set it to false
Edit:
I tried mmlooloo second part, but absolutly noting happend. Not even part 3. So I tried part 4, but i gave me an exception.
"This Activity already has an action bar supplied by the window decor.
Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to
false in your theme to use a Toolbar instead."
I removed the Window.FEATURE_ACTION_BAR request, but it gave me the same exception.
I don't think i need to set windowActionBar to false, but I did and it still gave me the same exception.
Any other options?
First:
Window provided Progress Bars are now deprecated with Toolbar.
Second:
you must use:
setSupportProgressBarIndeterminateVisibility(true);
instead of
setProgressBarIndeterminateVisibility(true);
because you extends ActionBarActivity. (because you are using supportRequestWindowFeature instead of requestWindowFeature)
Third:
If it crashes this is a known issue.
If your library is updated sorry but it is now just a no-op:
setSupportProgressBarIndeterminateVisibility() crashing has been fixed
for a future release, in that it will be a no-op.
Fourth:
My solution:
use toolbar with progressBar widget:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_spinner);
progressBar.setVisibility(View.VISIBLE);
}
Layouts:
activity_main.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:orientation="vertical">
<include layout="#layout/toolbar"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
and
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/progress_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:indeterminate="true"
android:visibility="gone" />
</android.support.v7.widget.Toolbar>

How to create full screen view ( Android 4.0+ )

I tried to create a full screen layout to display a imageview. In the lower end devices (below 4.0) i achieved this, unfortunately when i using the same code in higher end devices the system bar won't be hide. When I used this feature rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); (rootView - my fragment view), the system bar is hidden at the moment of application launch, once i touch the screen the system bar is enabled. How can i disable the system bar appearing on user touch until the current activity getting closed?
I think the trick is just to call the native style from Android allowing you to hide the ActionBar by default. Then just select the theme you need to use Holo Light, Holo Dark, etc.).
For example:
public class PhotoDialogFragment extends DialogFragment {
(...)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
//HERE IS THE TRICK
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light_NoActionBar_Fullscreen);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fullImageView = inflater.inflate(R.layout.photo_fragment, null);
(...)
return fullImageView;
}
(...)
Here is my "photo_fragment" XML 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" >
<ImageView
android:id="#+id/fullscreen_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/your_image" />
</LinearLayout>

How to implement master and child page like Activity in Android?

I want to place a common banner and menu on each Activity with footer too.
Can anyone guide me how can I implement master and child page like asp.net in Android???
Any help would be appreciated.
You could have each of your Activities extend a common base class which has a onCreateOptionsMenu method which inflates the menu from the same XML each time. Though as you can't have multiple inheritance, this may be tricky when you want to have plain activities and list activities, for example.
Another way would be to have a Util class where you have a method like setupMenu(Menu) which each of your Activities can call if you're doing some more complex menu setup.
In terms of the XML UI layout for each of your Activities, you can include a common banner by using the <include/> tag.
The solution was pretty easy.
You need to extends "Activity" Class,in onCreate function SetContentView to your base xml layout and also need to override setContentView in base Activity Class
For Example:
1.Create "base_layout.xml" with the below code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000"
android:padding="15dp" >
<LinearLayout android:orientation="horizontal" android:background="#000000"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="50dp" android:paddingLeft="10dp">
<ImageView android:layout_width="wrap_content" android:id="#+id/ImageView01"
android:adjustViewBounds="true" android:layout_height="wrap_content"
android:scaleType="fitCenter" android:maxHeight="50dp" />
</LinearLayout>
<LinearLayout android:id="#+id/linBase"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</LinearLayout>
2.Create "BaseActivity.java"
public class BaseActivity extends Activity {
ImageView image;
LinearLayout linBase;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.base_layout);
linBase = (LinearLayout)findViewById(R.id.linBase);
}
#Override
public void setContentView(int id) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(id, linBase);
}
}
and
public class SomeActivity extends BaseActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.some_layout);
//rest of code
}
}
The only thing I noticed so far was that when requesting a progress bar (requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)) this needs to be done before calling super.onCreate. I think this is because nothing can be drawn yet before calling this function.
This worked great for me and hopefully you will find this useful in your own coding.
I've had the same problem and solved it using ActivityGroup.
I suppose that menu items will move user to another activity, so with the same menu in every activity closing application with BACK button can be almost impossible (after some time user will have to go back through all activities he had ever seen).
I haven't found any good tutorials in english so have written mine some time ago (it's somewhat too short and in polish only, but Google Tranlslated version should be understandable) check this
You can also check how the TabHost works
ViewStub is the solution
activity_masterpage.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ViewStub android:id="#+id/stub_content"
android:inflatedId="#+id/subTree"
android:layout_width="match_parent"
android:layout_height="match_parent" />
stub = (ViewStub) findViewById(R.id.stub_content);
stub.setLayoutResource(R.layout.content_layout);
stub.inflate();

Categories

Resources