Ok i will try to be as detailed as i can here. I have an app with multiple activities in it. Some of the activities I want to have a custom title bar on with an Image, then text, then image button navigating back to the home(aka main activity). Most of the pages i want the custom title bar on are list view pages. I have successively been able to load the custom title bar but it doesn't load any text field or images just the title bar with nothing in it. here is the code that i've used.
window_title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#660000">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon72" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.39"
android:gravity="right|center_vertical"
android:paddingRight="5dip" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="#string/app_name"
android:textColor="#FFFFFF"
android:paddingRight="5dip" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#660000"
android:src="#drawable/home" />
<ImageView
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
custom_style.xml
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#660000</item>
</style>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleSize">50dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
Classes.Java
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Classes extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
setListAdapter( new ArrayAdapter<String>(this, R.layout.ministries, list));
ListView list = getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener(){
I'm not sure what i'm doing wrong or where i need to make changes but I would like to be able to have this custom title bar on some of the activities and load the name of that activity in the title bar. I've looked at several tutorials and these two have helped but i'm kinda stuck a bit. http://staticallytyped.wordpress.com/2011/03/18/android-dynamic-and-custom-title-bars/
and this one has helped the most and what i basicly followed
http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/
Thank you for any help you can give. I would have posted pics but haven't gotten that far on here yet so thank you again for any help
edit
the top image is what i want the title bar to look like and the bottom is what it looks like in app
YOu can use include tag of android to add your window_title.xml....
Refer :
android include tag - invalid layout reference
http://developer.android.com/resources/articles/layout-tricks-merge.html
Related
I am trying to show a simple listView with an image, a large text and a small text in each list item. I have coded the following but when I run the app, it gives a NullPointerException. For the method i have used, I do not need to findviewbyid so I'm not sure what I am doing to give this error. Anyone know?
This is my MenuPage.java file
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
public class MenuPage extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[]values=new String[]{"Coffee","Steak","Ice Cream"};
String[]prices=new String[]{"$4.99","$21.99","$12.99"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.values, values));
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.prices, prices));
}
}
this is my menu.xml for the java file above:
<?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">
<ImageView
android:id="#+id/ic_launcher_movies"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="4dp"
android:layout_marginRight="10px"
android:layout_marginTop="2dp"
android:src="#drawable/items"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageView>
<TextView
android:id="#+id/values"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/values"
android:textSize="25sp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="64dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:minHeight="120dp">
</TextView>
<TextView
android:id="#+id/prices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/prices"
android:textSize="10sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="120dp"
android:layout_marginLeft="65dp">
</TextView>
also, I created an xml with the drawables for the images i want to use;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:duration="30"
android:drawable="#drawable/coffee"/>
<item
android:duration="30"
android:drawable="#drawable/steak"/>
<item
android:duration="30"
android:drawable="#drawable/icecream"/>
and my logcat gives this error
any help would be appreciated! thank you
I think the problem in the menu layout file
android:text="#+id/values"
try to write some text or specify link to string resource like
android:text="#string/my_button_text"
And when you call setListAdpate two times you ovirwrite first adapter with new. Have a look to example here
ListActivity
Google's Material Design text field guidelines present floating labels for text input:
With floating inline labels, when the user engages with the text input
field, the labels move to float above the field.
Simple question: what's the best way to implement floating labels (on Android 5.0+)? Can you easily do it with standard components like EditText, and if so, how? Or is it simpler to go with a 3rd party lib?
You can now use the official Android DESIGN Support Library (available from support library 22.2.0)
http://android-developers.blogspot.dk/2015/05/android-design-support-library.html
Add this dependency to start using the library:
compile 'com.android.support:design:22.2.0'
Wrap the EditText in a TextInputLayout.
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginLeft="32dp"
app:hintTextAppearance="#style/TextAppearence.App.TextInputLayout">
you can customize TextInputLayout style
<style name="TextAppearence.App.TextInputLayout" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/accentColor</item>
</style>
You can use this Library AndroidFloatLabel:
For most use, you can simply use the custom view in your XML layout,
specifying a label to use as both the EditText hint and the label
TextView with the android:hint property. Example:
<com.iangclifton.android.floatlabel.FloatLabel
android:id="#+id/float_label_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/example_label" />
You can also dynamically set the label with
floatLabel.setLabel("Custom Label") or
floatLabel.setLabel(R.string.custom_label).
Custom Layout
If you want to specify a custom layout to use, you can do something
like this:
<com.iangclifton.android.floatlabel.FloatLabel
android:id="#+id/float_label_custom_layout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/example_label"
android:layout="#layout/custom_float_label" />
Your custom layout should include a label TextView (id/float_label)
and an EditText (id/edit_text). Right now, the custom layouts are
extremely limited because the FloatLabel simply lays out the label and
the EditText and ignores all other views. This is very efficient but
also prevents you from creating a much more complex layout. Here's an
example:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="#id/float_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:textIsSelectable="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text|textAutoCorrect|textCapSentences|textAutoComplete" />
</merge>
try this,
in main.xml
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#4644aa">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="#3FFF"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.github.florent37.materialtextfield.MaterialTextField
android:layout_width="300dp"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="20dp"
app:mtf_cardCollapsedHeight="4dp"
app:mtf_image="#drawable/ic_mail_grey600_24dp"
>
<!--
app:mtf_animationDuration="1000"
app:mtf_cardColor="#color/cardview_dark_background"
app:mtf_labelColor="#android:color/holo_red_dark"
app:mtf_openKeyboardOnFocus="true"
-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#333"
android:hint="Email"
android:textColorHint="#666"
android:textSize="15sp" />
</com.github.florent37.materialtextfield.MaterialTextField>
</LinearLayout>
and in Main.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
//import com.crashlytics.android.Crashlytics;
//import io.fabric.sdk.android.Fabric;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Fabric.with(this, new Crashlytics());
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(0xFFFFFFFF);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
and u also use this library.
https://github.com/florent37/MaterialTextField.
I need to have a TextView that, when the user type in it a word, a dropdown list box is be shown exactly with the same phrases that Google Search will suggest if those words are typed in the search textbox of google.com site.
Is it possible to have this service. How?
I searched on the web with no luck.
Thank you all.
For the dropdown you can use:
Autocomplete Android
For show result you must call your own web service or cached data. If you need google results i don't know if google expose a public API to obtain it...
Hi Geltrude here i added the sample source code for you. Please try and let me know. Hope it should helpful for you. Thanks.
First create autocomplete_textview.xml
<?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:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="133dp"
android:ems="10"
android:hint="Search Name"
android:paddingLeft="10dp"
android:paddingTop="7dp"
android:singleLine="true" />
</RelativeLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
AutocompleteSample.java
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutocompleteSample extends Activity {
private static AutoCompleteTextView autoCompleteText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete_textview);
autoCompleteText = (AutoCompleteTextView)findViewById(R.id.autoCompleteText);
List<String> nameList = new ArrayList<String>();
nameList.add("Steve");
nameList.add("Bill");
nameList.add("Tom");
nameList.add("Arnold");
nameList.add("Vishva");
nameList.add("Durai");
nameList.add("John");
nameList.add("Sam");
nameList.add("Johnson");
nameList.add("Hentry");
nameList.add("Thamos");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, nameList);
autoCompleteText.setAdapter(dataAdapter);
}
}
When I make the spinner with very long text, the spinner button (not drop down menu), which usually shows the selected text and the triangle image(▼), doesn't show the triangle image.
I tested with normal (android.R.layout.simple_spinner_item) layout and multiline layout.
But neither did.
The right side of spinner button is the outside of the window.
(Sorry, I can't attach the image because I'm a new user.)
The sources are following;
SpinnerTestActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class SpinnerTestActivity extends Activity {
String[] mStrings = {"verrrrrrrrrry looooooooooooooong",
"abc loooooooooooooooooooooooooong",
"xyz"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner)findViewById(R.id.spinner1);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mStrings);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.multiline_spinner_item, mStrings);
spinner.setAdapter(adapter);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
multiline_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
Thank you for your observations on my question.
I found the answer.
add
spinner.getLayoutParams().width = spinner.getWidth();
after
spinner.setAdapter(adapter);
Have you defined the android:prompt attribute in your Android Spinner controller?
It will help you. Please see the following code:
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/planet_prompt"
/>
You can refer to the following link: Can an Android AsyncTask doInBackground be synchronized to serialize the task execution?
I was trying to create a row layout where each of my list item will have a template to follow. Currently I have this, which allows me to display a single line text in each list.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20sp"
android:padding="100dp" >
</TextView>
However, when I try to change it to allow me to add more items such as image buttons and more text field, it always doesn't allow me to compile.
I tried taking a piece of xml from a tutorial on developer.android's website,
<?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:orientation="vertical">
<TextView android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text2"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
But it throws me an error stating - The processing instruction target matching "[xX][mM][lL]" is not
allowed.
- error: Error parsing XML: XML or text declaration not at start of entity
Can someone help me with this? I am not too sure what this error means.
UPDATE:
what i am trying to do is to have a fragment like a side bar on the right which shows a list of items, with the first xml that i posted, i'm able to get the results i want, but i couldn't make any changes to it.
package com.project.test;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TestListFragment extends ListFragment {
String [] Items = {"Item A", "Item B", "Item C"};
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.listtemplate, Items));
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
You would not believe the solution to this... I had the same issue and the answer was "remove all the white spaces before start the xml first line"
Might be because you are missing the starting tag of the LinearLayout?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text2"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>