How I can show the navigational bar icon on the left of my customized action bar - android-layout

I have customized my action bar with the following:
Title at the center
Title with customized font from asset
Title does not have the application icon
What I want is the navigational drawer icon on left.
Right now you can see it is on the left of the title My App. I want to have my application title on center only without the navigational drawer icon.
I tried with ImageView inside my customized action bar layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_nav_drawr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left"
android:src="#drawable/ic_navbar_drawer" />
<TextView
android:id="#+id/apptitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_horizontal|center_vertical"
android:text="#string/app_name" />
</RelativeLayout>
my activity code snippet for the action bar customization is:
private void actionBarCustomizations(){
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
//getActionBar().setDisplayShowTitleEnabled(true);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.app_action_bar, null);
TextView actionBarTitleTextView = (TextView)v.findViewById(R.id.apptitle);
Typeface neoTechMedium =
Typeface.createFromAsset(getAssets(), "fonts/NeoTech-Bold.ttf");
actionBarTitleTextView.setTypeface(neoTechMedium);
ActionBar.LayoutParams params = new
ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
getActionBar().setCustomView(v, params);
}
Any one can help me on this please?

HI Remove layout_gravity, and gravity from Image View.
Just Use
android:layout_alignParentLeft="true"
It's in ImageView property Ok...

Related

Android Studio & Kotlin: Changing programmatically added images of a list view

Hey fellow programmers,
I was just programming in a listView and implementing actions for tapping a list element until I got to a point where I wanted to keep things recursively.
Taken this ClickListener, I would like to have the image next to the list element changed when tapped.
list.add(Model("firstelement","description 1", R.drawable.button_activated))
list.add(Model("secondelement","description 2",R.drawable.button_activated))
listview.adapter = MyAdapter(this, R.layout.row, list)
var elementArray: IntArray = intArrayOf(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
listview.setOnItemClickListener{ parent: AdapterView<*>, view: View, position:Int, id: Long ->
val currentimage = ? //here I'd like to retrieve the imageview of the tapped list item
val img: ImageView = findViewById(R.id.currentimage)
if (elementArray.get(position)==0){
img.setImageResource(R.drawable.button_activated)
elementArray.set(position,1)
}
else if (elementArray.get(position)==1){
img.setImageResource(R.drawable.button_dectivated)
elementArray.set(position,0)
The images are added to the list together with the text view.
How can I retrieve them to change them? Tried listview.getItemIdAtPosition but that leads to errors.
I bet there is a way - I just don't see it. Thanks in advance for any hint!
In addition, here is my layout row.xml where the list gets loaded into:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
>
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="#+id/image0"
android:src="#mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:text="first element"
android:textStyle="bold"
android:textSize="20sp"
android:layout_margin="5dp"
android:textColor="#000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:text="second element"
android:textSize="18sp"
android:layout_margin="5dp"
android:textColor="#a9a9a9"
/>
</LinearLayout>
</LinearLayout>
Just resolved it on my own with the help of this solution: Android: Dynamically change Image in Listview
Had to access the lower imageView of the current view: ImageView = view.findViewById(R.id.image)

DatePicker content is overlapped by title of custom dialog

I created custom dialog fragment to be able use Joda dates type.
Creating of dialog. It's method from my custom DialogFragment.
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString(KEY_TITLE, getString(R.string.dg_dp_title));
LocalDate date = (LocalDate) getArguments().getSerializable(KEY_DATE);
if (null == date) date = new LocalDate();
FragmentActivity context = getActivity();
#SuppressLint("InflateParams")
View view = LayoutInflater.from(context).inflate(R.layout.dialog_date_picker, null);
mDatePickerView = (DatePicker) view.findViewById(R.id.dg_dp_picker);
mDatePickerView.updateDate(date.getYear(), date.getMonthOfYear() - 1, date.getDayOfMonth());
return new AppDialogBuilder(context)
.setTitle(title)
.setView(view)
.setPositiveButton(R.string.dg_dp_button_positive, mListener)
.setNegativeButton(R.string.dg_dp_button_negative, mListener)
.create();
}
Layout file.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<DatePicker
android:id="#+id/dg_dp_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:calendarViewShown="true"
android:datePickerMode="calendar"
android:spinnersShown="false"/>
</FrameLayout>
If remove setting title form builder, then dialog will looks fine
But I need to use title. Thoughts?
You can try to make your own custom title layout, and adjust your title size
title_bar.xml (Custom title layout)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="15dp"
android:textStyle="bold"
/>
</LinearLayout>
Now you must set your dialog with your custom title :
datePickerDialog.setCustomTitle(view_of_your_custom_title_layout);

ListView row height and another interesting issue of selective focus (when the parent is scrolled)

First of all, excuse me if it sounds duplicate. I have visited so many threads but couldn't find a suitable answer of my problem. I have tried ScrollingMovementMethod, android:scrollbar, wrap_content in the parent and many other things suggested in those threads but nothing worked for me. Feel free to edit the title as I couldn't find a better one.
Problem Description
I have a list view and each row of the listview has three controls 1. Image View (to Show the contact image) 2. TextView (to show the Contact name) 3. TextView (to show the status message of the contact (if available)).
What I have tried:-
1. XML 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="match_parent" >
<ImageView
android:id="#+id/icon"
android:layout_width="22dp"
android:layout_height="50dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="15dp"
android:contentDescription="#string/strBuddyImage" >
</ImageView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="56dp" >
<TextView
android:id="#+id/uNameTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/customMsg"
android:minHeight="?android:attr/listPreferredItemHeight"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/fontSizeListRowHeader" />
<TextView
android:id="#+id/customMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:maxLines="4"
android:textSize="#dimen/fontSizeListRowText"
android:textStyle="italic" />
</RelativeLayout>
</LinearLayout>
Adapter Which extends ArrayAdapter (which only overrides the getView method)
static class ViewHolder {
public ImageView imageView;
public TextView uNameTxtView;
public TextView custMsgTxtView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// Log.d("BuddyListAdapter", "Inside getView() " + position);
if ( rowView == null ) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.buddy_listview_row_layout, parent, false);
viewHolder = new ViewHolder();
viewHolder.uNameTxtView = (TextView) rowView.findViewById(R.id.uNameTxt);
viewHolder.imageView = (ImageView) rowView.findViewById(R.id.icon);
viewHolder.custMsgTxtView = (TextView) rowView.findViewById(R.id.customMsg);
rowView.setTag(viewHolder);
}
viewHolder = (ViewHolder) rowView.getTag();
synchronized (buddyList) {
buddy = buddyList.get(position);
}
viewHolder.uNameTxtView.setText(buddy.getDisplayName());
viewHolder.custMsgTxtView.setText(buddy.getCustomMessage());
// Change the icon for users who are offline
if ( buddy.getState() == 0 ) {
viewHolder.imageView.setImageResource(R.drawable.offline);
} else {
viewHolder.imageView.setImageResource(R.drawable.online);
}
// rowView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return rowView;
}
What I get
Desired
I want to show all the rows of list view of same size and if someone has a bigger Status message (Which needs multiple lines) I would like to show the name of the person and first few lines (say 4) of the status message. No matter how big the status message is name should be always visible to the user.. How can I do that ?
Please point if any other mistake you find in the code.
One thing i noticed is you use a lot of hardcoded dimensions,is that really necesary? using wrap_content would be better practice on most layouts,that way you let android handle it.
As far as your question is concerned you can do two things:
1)Your two EditTexts are inside a relative layout,and if customMsg is 4 lines long,it will get scrolled ,and you'll loose your uNameTxt so you should limit customMsg more,to something like two lines,or less. OR if you are ok with loosing some parts of the code you could adapt your layout this way
<TextView
android:id="#+id/uNameTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout_alignParentTop="true"
android:minHeight="?android:attr/listPreferredItemHeight"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/fontSizeListRowHeader" />
<TextView
android:id="#+id/customMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/uNameTxt"
android:maxLines="4"
android:textSize="#dimen/fontSizeListRowText"
android:textStyle="italic" />
it should scroll down no more and you should see Name and a part of customMsg
2)Edit your layout to something more efficient like:
<?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:minHeight="56dp">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="15dp"
android:contentDescription="sasd" >
</ImageView>
<TextView
android:id="#+id/uNameTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:singleLine="true"
android:layout_toRightOf="#+id/icon"
android:textSize="#dimen/fontSizeListRowHeader"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/customMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/uNameTxt"
android:maxLines="2"
android:layout_toRightOf="#+id/icon"
android:textSize="#dimen/fontSizeListRowText"
android:textStyle="italic" />
</RelativeLayout>
I agree with sokie on the use of wrap_content. I tend to use LinearLayout a lot since it provides a consistent layout on various display sizes. One way of implementation is as follows. By replacing your inner RelativeLayout to LinearLayout, your text view containing the name will always be displayed and the message text view will display the maximum possibles lines (or can be limited by specifying the maxLines) with the available remaining/space. If you would like to display the whole status message, then you can achieve this by changing the parent LinearLayout height parameter to wrap_content.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="56dp"
android:minHeight="?android:attr/listPreferredItemHeight">
<ImageView
android:id="#+id/icon"
android:layout_width="22dp"
android:layout_height="50dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="15dp" >
</ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/uNameTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/customMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="4"
android:textStyle="italic"
/>
</LinearLayout>
</LinearLayout>

Set default text style when using AlertDialog.Builder.setCustomTitle()

This might be easy but I can't find the answer anywhere.
I'm using setCustomTitle for my alert dialog in order to use my own view for the dialog title:
View customTitle = getLayoutInflater().inflate(R.layout.dialog_title, null);
new AlertDialog.Builder(AddBusActivity.this).setCustomTitle(customTitle).(some more stuff).show();
And dialog_title.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="My Title"
android:id="#+id/title"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="right|center_vertical">
</TextView>
</LinearLayout>
My problem is making the title TextView use the default dialog title style.
In my device the alert dialog background is dark, so text has to be bright, but in other devices it might be different.
I tried adding android:textAppearance="#android:style/TextAppearance.DialogWindowTitle" to the TextView, but it caused the text to be black which isn't the right style.
How can I solve this?
I had similar problem with styles and colors, but resolved it by using custom Dialog instead of AlertDialog.
1) my code with
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(mName);
...
mDialog = builder.create();
mDialog.show();
was changed to
dialog = new Dialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_custom_title);
// initializing Views
dialog.show();
2) in layout.dialog_custom_title set needed styles (background and color)
<LinearLayout xmlns: android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dialogs_background"
android:orientation="vertical" >
...
<TextView
...
android:textColor="#color/TxtLightBlue"
android:textSize="#dimen/text_size_default"
/>
...
Hope it will help someone.
I used the code below whiuch set the colors and the spacing quite well
<TextView android:text="My Title"
android:id="#+id/title"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="24sp"
android:textColor="#color/green"
android:textStyle="bold"
android:paddingBottom="#dimen/activity_vertical_margin"
android:gravity="center_horizontal|center_vertical">
</TextView>

Android - UI Disappear after startActivity

I'm starting an simple ACTION.VIEW activity to show up the web browser.
But when the user press the "back" key, it returns to my initial application. The application is working perfectly except that all the main UI elements have disappeared.
Anyone knows why ?
Here's how I start the web browser :
//Go to web page
ImageButton web = (ImageButton) _gears.findViewById(R.id.Web);
web.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
try
{
String url = "http://apps.toonboom.com/flip-boom-lite-ipad";
Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.parse(url);
i.setData(u);
_mainActivity.startActivity(i);
}
catch (ActivityNotFoundException e)
{
// Raise on activity not found
Toast.makeText(_mainActivity.getApplicationContext(), "Browser not found.", Toast.LENGTH_SHORT);
}
}
});
When coming back from that browser page, onStart() and onResume() are called normally. What I don't understand is that the back and home button lifecycle works perfectly. The user can manually go away of the app and come back without any UI problems. The problem only occurs when coming back from that startActivity() call ?
Also, I don't need to retain any specific UI values....I just want them to be present in the layout ;)
EDIT
I have a gles view that I use to draw and I think it display itself in front of the other UI elements....But I don't understand why if that is the case....
Here's a piece of the xml and the onCreate method :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/MainLayout">
<!-- This is a dummy layout so we can add our custom GlView dynamically to this relative position -->
<LinearLayout
android:id="#+id/SurfaceViewLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<!-- This is a dummy layout so we can add the timeline dynamically to this relative position -->
<HorizontalScrollView
android:id="#+id/TlScroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="0dip"
android:padding="0dip"
android:scrollbars="none"
android:fillViewport="false"
android:scrollbarFadeDuration="0"
android:scrollbarDefaultDelayBeforeFade="0"
android:fadingEdgeLength="0dip"
android:scaleType="centerInside">
<!-- HorizontalScrollView can only host one direct child -->
<LinearLayout
android:id="#+id/TimelineContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_margin="0dip"
android:padding="0dip"
android:scaleType="centerInside"/>
</HorizontalScrollView >
<RelativeLayout
android:id="#+id/BottomTools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/TlScroller" >
<ImageButton
android:id="#+id/PaletteBtn"
android:layout_width="30dip"
android:layout_height="30dip"
android:background="#android:color/transparent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dip"
android:layout_marginBottom="60dip"
android:src="#drawable/palette44x44"
android:scaleType="centerInside"/>
<RelativeLayout
android:id="#+id/PadLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:src="#drawable/pad_btn"
android:scaleType="centerInside">
<ImageButton
android:id="#+id/PadBtn"
android:layout_width="75dip"
android:layout_height="75dip"
android:background="#android:color/transparent"
android:layout_centerInParent="true"
android:src="#drawable/pad_btn"
android:scaleType="centerInside"/>
<TextView
android:id="#+id/FrameCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dip"
android:padding="0dip"
android:layout_centerInParent="true"
android:textColor="#000000"
android:text="#/x"/>
</RelativeLayout>
<ImageButton
android:id="#+id/PreviousBtn"
android:layout_width="40dip"
android:layout_height="40dip"
android:background="#android:color/transparent"
android:layout_toLeftOf="#+id/PadLayout"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dip"
android:src="#drawable/previous_btn"
android:scaleType="centerInside"
android:scaleHeight="30%"
android:scaleWidth="30%"/>
// Goes like that with other ImageButton till the end bracket
= = =
#Override protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
//Set the main layout element
setContentView(R.layout.ui);
// Then most buttons are created like this . . .
//Create the tools pop-up menu
ImageButton toolBtn = (ImageButton) this.findViewById(R.id.CurrentToolBtn);
_tools = new ToolsPopup(toolBtn);
}
When you launching a web browser it uses a lot of memory so android killing your app, when it is going back to activity it recreates it. So you should save your dynamically added views using onsaveinstancestate and onrestoreinstancestate.
onSaveInstanceState () and onRestoreInstanceState ()

Resources