Im designing my interfaces for my research project on android. So I added facebook kind side navigation listview. But when the itesms are added to the list view it will not get align center horizontally..I need to get the icon origin and the title in a same horizontal line. Please be kind enough to provide me a solution for my problem. Below provided my code. I'm sorry about my english.
Here is an image of my current interface. Please refer settings button. The settings word is bit upside. I need both icon and settings word aligh horizontally in same line.
image is here http://sphotos-e.ak.fbcdn.net/hphotos-ak-ash4/485944_4792129535506_1863888545_n.jpg
slide.xml - all the items for the listview are included here
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item_one"
android:icon="#drawable/settings"
android:title="#string/item_one"
android:top="30dp"
>
</item>
<item
android:id="#+id/item_two"
android:icon="#drawable/ic_launcher"
android:title="#string/item_three">
</item>
<item
android:id="#+id/item_three"
android:icon="#drawable/ic_launcher"
android:title="#string/item_three">
</item>
<item
android:id="#+id/item_four"
android:icon="#drawable/ic_launcher"
android:title="#string/item_four">
</item>
<item
android:id="#+id/item_five"
android:icon="#drawable/ic_launcher"
android:title="#string/item_one">
</item>
<item
android:id="#+id/item_six"
android:icon="#drawable/ic_launcher"
android:title="#string/item_two">
</item>
<item
android:id="#+id/item_seven"
android:icon="#drawable/ic_launcher"
android:title="#string/item_three">
</item>
<item
android:id="#+id/item_eight"
android:icon="#drawable/ic_launcher"
android:title="#string/item_four"
>
</item>
</menu>
slidemenu.xml - the listview is here
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="260dip"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#2c323f"
>
<ImageView
android:id="#+id/menu_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
/>
<ListView
android:id="#+id/menu_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:background="#2c323f"
android:fadingEdge="none"
android:overScrollMode="never"
android:listSelector="#454b5d"
android:divider="#layout/divider"
android:dividerHeight="1sp"
android:cacheColorHint="#2c323f"
/>
</LinearLayout>
<FrameLayout
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
here is my activity class
package com.coboltforge.slidemenuexample;
import com.coboltforge.slidemenu.SlideMenu;
import com.coboltforge.slidemenu.SlideMenu.SlideMenuItem;
import com.coboltforge.slidemenu.SlideMenuInterface.OnSlideMenuItemClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnSlideMenuItemClickListener {
private SlideMenu slidemenu;
private final static int MYITEMID = 42;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* There are two ways to add the slide menu:
* From code or to inflate it from XML (then you have to declare it in the activities layout XML)
*/
// this is from code. no XML declaration necessary, but you won't get state restored after rotation.
// slidemenu = new SlideMenu(this, R.menu.slide, this, 333);
// this inflates the menu from XML. open/closed state will be restored after rotation, but you'll have to call init.
slidemenu = (SlideMenu) findViewById(R.id.slideMenu);
slidemenu.init(this, R.menu.slide, this, 333);
// this can set the menu to initially shown instead of hidden
// slidemenu.setAsShown();
// set optional header image
slidemenu.setHeaderImage(getResources().getDrawable(R.drawable.ic_launcher));
// this demonstrates how to dynamically add menu items
SlideMenuItem item = new SlideMenuItem();
item.id = MYITEMID;
item.icon = getResources().getDrawable(R.drawable.ic_launcher);
item.label = "Dynamically added item";
slidemenu.addMenuItem(item);
// connect the fallback button in case there is no ActionBar
Button b = (Button) findViewById(R.id.buttonMenu);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
slidemenu.show();
}
});
}
public void onSlideMenuItemClick(int itemId) {
switch(itemId) {
case R.id.item_one:
Toast.makeText(this, "Item one selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item_two:
Toast.makeText(this, "Item two selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item_three:
Toast.makeText(this, "Item three selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item_four:
Toast.makeText(this, "Item four selected", Toast.LENGTH_SHORT).show();
break;
case MYITEMID:
Toast.makeText(this, "Dynamically added item selected", Toast.LENGTH_SHORT).show();
break;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home: // this is the app icon of the actionbar
slidemenu.show();
break;
}
return super.onOptionsItemSelected(item);
}
}
the settings word is bit upside. I need both icon and settings word
aligh horizontally in same line
I'm guessing you use this library. The problem is that you can't access the layout file that is used for the row view of the sliding menu adapter to modify it. But you can easily solve this if you copy the library's code(which consists of very few files) and put it directly in your project. Then modify the slidemenu_listitem.xml file like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/menu_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:layout_marginTop="10dip" />
<TextView
android:id="#+id/menu_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:textSize="24dp" />
</LinearLayout>
Of course, you can always make a feature request to the user how made the library to modify it so you can insert your own layout file in the library(and wait for that to happen).
You can use the
android:gravity="center" (or top/bottom/left/right depending your needs)
of your textview to align text where you want.
layout_gravity sets the gravity (alignment) of the View or Layout
inside its parent. If you have a text view inside a linear layout,
then setting the layout_gravity of the text view will alter its
alignment in the layout. gravity sets the alignment of the content of
the view or layout it is applied on. If you want your text (in the
text view) to be left aligned, center, or right aligned, then modify
the gravity attribute of the text view.
From: http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
Related
I have two ToggleButton in my activity. I want to click on the first button and have its color change to white. If I click on the second button, the color of first button should change to black and the color of second button change to white.
I want to know which button is selected. How can I do this with ToggleButton, or with something else?
<ToggleButton android:id="#+id/tg_btn1"
android:layout_width="match_parent"
android:layout_height="46px"
android:background="#ffffff" />
<ToggleButton android:id="#+id/tg_btn1"
android:layout_width="match_parent"
android:layout_height="46px"
android:background="#ffffff" />
Please help me, I would appreciate that.
use a Style for your Toggle Button
<style name="ToggleButton.YourTheme" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#color/your_color</item>
<item name="colorControlActivated">#color/your_color</item>
</style>
Apply that to your button and it should work. Have not tested it recently.
<ToggleButton android:id="#+id/tg_btn1"
android:layout_width="match_parent"
android:layout_height="46px"
android:theme="#style/ToggleButton.YourTheme"
android:background="#ffffff" />
In order to have two ToggleButtons in an activity which, when ToggleButton1 is clicked, ONLY that button changes color and the other one does not, but then when the ToggleButton2 is switched on: the ToggleButton1 turns "off" and ToggleButton2 turns "on", I created two global variables: One boolean for the first toggle, and a boolean for the second toggle.
package com.example.micha_000.togglecolors;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
Boolean toggleOneOn = false;
Boolean toggleTwoOn = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ToggleOne(View view){
//If the Toggle Button is off
if(!toggleOneOn){
//This view element references the ToggleButton1
view.setBackgroundColor(Color.WHITE);
toggleOneOn = true;
}
//If it is is clicked while on
else{
view.setBackgroundColor(Color.BLACK);
toggleOneOn = false;
}
}
public void ToggleTwo(View view){
//If the Toggle Button is clicked while off
if(!toggleTwoOn){
//This view element references the ToggleButton2
view.setBackgroundColor(Color.WHITE);
//This ToggleButton element references the ToggleButton1
ToggleButton toggle1 = findViewById(R.id.ToggleButton1);
toggle1.setBackgroundColor(Color.BLACK);
toggleOneOn = false;
toggleTwoOn = true;
}
//If it is is clicked while on
else{
view.setBackgroundColor(Color.BLACK);
toggleTwoOn = false;
}
}
}
I then used the "onClick" property in xml to reference the ToggleOne and ToggleTwo methods I created in my java class (Those methods must be public, void, and have a View as a parameter as they do in my code). I then have conditionals checking those global booleans, and then using "setBackgroundColor" to change the color of the toggle appropriately.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#color/colorPrimary"
tools:context=".MainActivity">
<ToggleButton
android:id="#+id/ToggleButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:onClick="ToggleOne"
android:background="#000000"
android:layout_marginBottom="5dp"/>
<ToggleButton
android:id="#+id/ToggleButton2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:onClick="ToggleTwo"
android:background="#000000"
android:layout_below="#+id/ToggleButton1"/>
</RelativeLayout>
If you wanted to switch the other toggle button when one is pressed, you just need to adjust what happens inside the if/else statements inside the toggleButton methods
I'm learning actionviews and was following this short tutorial:
http://wptrafficanalyzer.in/blog/adding-custom-action-view-to-action-bar-in-android/
But I don't know why the EditText object becomes null at the runtime. Please help me with this.
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Create an option menu from res/menu/items.xml */
getMenuInflater().inflate(R.menu.items, menu);
/** Get the action view of the menu item whose id is search */
View v = (View) menu.findItem(R.id.search).getActionView();
/** Get the edit text from the action view */
EditText txtSearch = ( EditText ) v.findViewById(R.id.txt_search);
/** Setting an action listener */
txtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Toast.makeText(getBaseContext(), "Search : " + v.getText(), Toast.LENGTH_SHORT).show();
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
items.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/photo"
android:title="Photo"
app:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/video"
android:title="Video"
app:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/mobile"
android:title="Mobile"
app:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/search"
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
android:actionLayout="#layout/search_layout"
/>
</menu>
search_layout.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="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/txt_search"
android:inputType="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
In your comment you stated the xml file names are search_layout.xml and items.xml. Now, in your code, you'll notice this line:
setContentView(R.layout.activity_main);
And this is important, so listen up: This line of code basically says, here is the corresponding xml file (layout resource element) that belongs to this java class. Now, what is the point of this? Well, if you want to access something that is in the content of an xml file called hello, and change the image of an image button, you cant do it until you specify where that image button is.
XML: I'm what the screen looks like!
Java: I'm what to do with some screen. What screen? Well, that screen is described in the setContentView line.
So, as you go deeper into android, you will start to get more and more XML files, and you need to specify which one you are talking about, so that the java (which is the brain of your program) knows what to do and where to do it.
Hope this helped, let me know if it did!
Ruchir
Your setContentView(R.layout.activity_main); layout is referencing to the wrong file. Change to (i guess is that the name of the file) setContentView(R.layout.search_layout);
I have a ListView, where each item has a custom LinearLayout with a bg image, a textView and 2 imageViews.
Now I need that while the user is touching the item, all of those switch to the "pressed" state:
the bg image of the LiearLayout must be replaced with another one
the TextView should change textColor
both ImageViews in the item should switch to alternative images
Normally such stuff would be done using an xml resource with selector inside, e.g. the LinearLayout would use a drawable with selector inside for background, the TextView a drawable with selector and colors for textColor, and ImageViews use selector with images inside for src.
The problem is that the pressed state is only detected by the LinearLayout and not by the child views (?), so only the background image changes.
I've tried implementing this using OnTouchListener, but then comes the problem that I can't securely get access to Views inside the list item.
I tried caching the view which I return in getView() of the list item to then later change the images and text color. This works usually, but e.g. if one of the list items opens another activity, then the view somehow gets lost and the highlighted state stays indefinitely. I've tried debugging and it works correctly if I step thru with the debugger.
Also, reusing the cachedView seems to bring no good and messes things up completely, so I'm just inflating a new view for the list item each time (this must be inefficient).
Just in case, here is the code of the custom list item item i'm using for the custom list adapter:
public class MyListItem extends AbstractListItem
{
private int iconResource, iconHighlightedResource;
private int textResource;
private View.OnClickListener onClickListener;
private LinearLayout currentView;
private ImageView imgIcon;
private TextView txtText;
private ImageView imgArrow;
private boolean bIsHighlighted;
public MyListItem(int iconResource, int iconHighlightedResource, int textResource, View.OnClickListener onClickListener)
{
this.iconResource = iconResource;
this.iconHighlightedResource = iconHighlightedResource;
this.textResource = textResource;
this.onClickListener = onClickListener;
}
public View getView(View cachedView)
{
this.currentView = buildView();
populateView();
update();
return this.currentView;
}
private LinearLayout buildView()
{
LayoutInflater inflater = (LayoutInflater)App.get().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return (LinearLayout)inflater.inflate(R.layout.my_menu_item, null);
}
private void populateView()
{
this.imgIcon = (ImageView)this.currentView.findViewById(R.id.img_menu_item_icon);
this.txtText = (TextView)this.currentView.findViewById(R.id.txt_menu_item_text);
this.txtText.setText(this.textResource);
this.txtText.setTypeface(App.fontCommon);
this.imgArrow = (ImageView)this.currentView.findViewById(R.id.img_menu_item_arrow);
this.currentView.setOnClickListener(this.onClickListener);
this.currentView.setOnTouchListener(this.highlighter);
}
private View.OnTouchListener highlighter = new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
int nAction = event.getAction();
int nActionCode = nAction & MotionEvent.ACTION_MASK;
switch (nActionCode)
{
case MotionEvent.ACTION_DOWN:
bIsHighlighted = true;
update();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
bIsHighlighted = false;
update();
break;
}
return false;
}
};
private void update()
{
if (this.bIsHighlighted)
{
updateForHighlightedState();
}
else
{
updateForNormalState();
}
}
private void updateForHighlightedState()
{
Resources r = App.get().getResources();
this.currentView.setBackgroundResource(R.drawable.button_beveled_m_call_to_action_taking_input);
this.imgIcon.setImageResource(this.iconHighlightedResource);
this.txtText.setTextColor(r.getColor(R.color.white));
this.imgArrow.setImageResource(R.drawable.arrow_highlighted);
}
private void updateForNormalState()
{
Resources r = App.get().getResources();
this.currentView.setBackgroundColor(r.getColor(R.color.white));
this.imgIcon.setImageResource(this.iconResource);
this.txtText.setTextColor(r.getColor(R.color.text_dark));
this.imgArrow.setImageResource(R.drawable.arrow);
}
}
Here is the layout file (xml):
<?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="wrap_content"
android:orientation="horizontal"
android:background="#color/white"
android:gravity="center_vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/img_menu_item_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/info" />
<TextView
android:id="#+id/txt_menu_item_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24dp"
android:text="Menu item"
android:textColor="#color/text_dark"
android:layout_marginLeft="5dp" />
<ImageView
android:id="#+id/img_menu_item_arrow"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/arrow" />
</LinearLayout>
After lots of experimenting finally this worked:
Every child view inside the list item layout must have android:duplicateParentState="true".
Then all of them can just use selector drawables. No extra effort inside the code is required.
<?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="wrap_content"
android:orientation="horizontal"
android:background="#drawable/my_item_bg"
android:gravity="center_vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/img_menu_item_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/selector_info"
android:duplicateParentState="true" />
<TextView
android:id="#+id/txt_menu_item_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24dp"
android:text="Menu item"
android:textColor="#drawable/selector_color_my_button_text"
android:layout_marginLeft="5dp"
android:duplicateParentState="true" />
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/selector_arrow"
android:duplicateParentState="true" />
</LinearLayout>
You should create custom drawable selectors, and set them as the background to your listview element.
Step#1, create another layout (named: layout_selected for this example), with the appropriate background color for your pressed state (like the layout file you supplied, but with the background attribute of the linear set to another color).
Then you will define a drawable selector, which will be placed in your drawable folder), defining which background should be use in which instance. This will look something like this:
<!-- pressed -->
<item android:drawable="#drawable/layout_selected" android:state_pressed="true"/>
<!-- focused -->
<item android:drawable="#drawable/layout_normal" android:state_focused="true"/>
<!-- default -->
<item android:drawable="#drawable/layout_normal"/>
Finally, to use this in your list, when you set the layout for your adapter, set it to the selector we just created, instead of your standard layout.
Maybe a little hard to explain, but you want to use 'Drawable Selectors' to accomplish what you want.
I would suggest to add ViewHolder pattern for listview. This will optimize your listview drawing & creating UI.
Also in that we can use setTag to save instance of row. In that you can handle touch event.
I am having problem in starting a new activity on clicking a button, here's my code:
package test.project;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
public class TestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View aboutButton = findViewById(R.id.about_content);
aboutButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_content:
Intent i = new Intent(this, testit.class);
startActivity(i);
break;
// More buttons go here (if any) ...
}
}
}
Can anyone please help me correct this error
Error Line
aboutButton.setOnClickListener(this);
Main.xml file
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here" android:layout_gravity="center"
android:text="Click here" android:layout_gravity="center" android:layout_marginTop="30dip"/>
</LinearLayout>
XML file containing about_content is
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TextView
android:id="#+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" />
</ScrollView>
about_content is already defined here
<TextView
android:id="#+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" />
Well, you didn't post your logcat output, but since this is such a common beginner's mistake, I'm going to take a wild guess and say that you are probably getting a NullPointerException.
Your call to findViewById is probably returning null, which means that the system could not find the view associated with the id given by R.id.about_content. I would double check your XML layout for typos.
Odds are you don't have anything with the id about_content in main.xml, which will create a NullPointerException.
Also, if aboutButton is supposed to be a traditional Button, then you should use this:
Button aboutButton = (Button) findViewById(R.id.about_content);
Addition
Since aboutButton is a TextView, use this:
TextView aboutButton = (TextView) findViewById(R.id.about_content);
but this TextView must be in the layout passed to setContentView() or findViewById() will return null.
That is because the "main.xml" which you have set your content view ... does not contain the about_content TextView, its in the other xml which you have posted ...
Note: You can access only those R.id's which are present in your setContentView(R.layout.yourlayout) xml ...
you make setContentView(R.layout.main); but main.xml does not include View have id = R.id.about_content. If you raplace by findViewById(R.id.button1); It will work.
This is the solution for
Button aboutButton = (Button)findViewById(R.id.about_content);
And dont forget to add testit Activity in Android Manifest
I'm very new to programming in Android, but have been struggling all day with a problem and would appreciate your help.
I'm trying to create a form to get user information (essentially a new contact) which is accessed from the menu. When I click the button to create the new form, I get the following error:
"Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#626fd5e0"
This is my code:
The menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="#string/settings_search"
android:alphabeticShortcut="#string/settings_shortcut_search" />
<item android:id="#+id/new_contact"
android:title="#string/settings_new"
android:alphabeticShortcut="#string/settings_shortcut_new" />
</menu>
The code for calling the new activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_contact:
//activities to create a new account
//startActivity(new Intent(this, Prefs.class));
Intent i = new Intent(MainActivity.this, NewContact.class);
startActivity(i);
//addSaver("String");
return true;
case R.id.search:
return true;
// More items go here (if any) ...
}
return false;
}
The view for the new contact activity:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">"
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="top">
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a test\nsecond line\n"/>
<Button
android:id="#+id/new_contact_button"
android:layout_height="wrap_content"
android:text="#string/submit" />
</LinearLayout>
</ScrollView>
And the code for the new activity:
public class NewContact extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_contact);
// Set up click listeners for all the buttons
View newContactButton = findViewById(R.id.new_contact_button);
newContactButton.setOnClickListener(this);
}
Bizarrely if I comment out the button from the class and from the xml then it works correctly and opens up the new activity (but of course I can't do anything with that activity).
In your button xml code, you are missing
android:layout_width="wrap_content"
add that and give it a shot.