Make a normal layout look like a PreferenceScreen - android

The PreferenceScreen isn't good enough for me, since I've to add items to a Spinner. Those items need to come from a data list.
I've got a custom ArrayAdapter that returns the name of the item, and when I click it. It returns the data that is contained within the item.
I want to use that same ArrayAdapter in a ListPreference (that's the spinner in the PreferenceScreen) but the ListPreference doesn't allow me to use a Adapter.
So, I want to recreate the look of the PreferenceScreen (with the PreferenceCategory's) without the use of the actual PreferenceScreen (and PreferenceCategory's)
Is this possible with a library? I haven't found one.
Thanks,
Tim

I tried to collect my first method - I hope I didn't forget to include some parts (aapart color definitions or statelist drawables, which is a trivial task to make your own)
Customizing the standard Preferences
/res/xml/prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<!-- ... -->
<PreferenceCategory android:title="#string/pref_vibrate_cat">
<CheckBoxPreference
android:persistent="true"
android:key="vibrate"
android:title="#string/pref_vibrate_title"
android:summary="#string/pref_vibrate_summ"
android:defaultValue="true"
android:layout="#layout/prefs"
/>
</PreferenceCategory>
<!-- ... -->
<!-- Just to show how to use a custom preference (you must have the corresponding java Class in your project) -->
<PreferenceCategory android:title="#string/pref_tts_cat">
<com.dergolem.abc.CLS_Prefs_Multi
android:persistent="true"
android:key="tts"
android:title="#string/pref_tts_title"
android:summary="#string/nothing"
android:dialogTitle="#string/pref_tts_dlg"
android:dialogIcon="#android:drawable/sym_action_chat"
android:entries="#array/prefs_tts_titles"
android:entryValues="#array/prefs_tts_values"
android:defaultValue="#array/prefs_tts_defaults"
android:layout="#layout/prefs"
android:widgetLayout="#layout/arr_dn"
/>
</PreferenceCategory>
<!-- ... -->
</PreferenceScreen>
/res/layout/prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a visually child-like Preference in a PreferenceActivity. -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingStart="16dp"
android:paddingEnd="?android:attr/scrollbarSize"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="16dp"
android:gravity="center"
android:orientation="horizontal"
>
<ImageView
android:id="#+android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"
>
<TextView
android:id="#+android:id/displayTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
/>
<TextView
android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
/>
<TextView
android:id="#+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignStart="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:shadowColor="#color/white"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="1"
android:maxLines="4"
/>
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout
android:id="#+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="48dp"
android:gravity="center"
android:orientation="vertical"
/>
</LinearLayout>
/src/ACT_Prefs
package com.dergolem.abc;
/* ---------------------------------- Imports ------------------------------- */
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.ListView;
public final class ACT_Prefs // NO_UCD (use default)
extends PreferenceActivity
implements OnSharedPreferenceChangeListener
{
/* ------------------------------ Objects ------------------------------- */
private Context ctx = null;
/* ----------------------------- Overrides ------------------------------ */
// Reload the Activity on rotation.
#Override
public final void onConfigurationChanged(final Configuration cfg)
{
super.onConfigurationChanged(cfg);
reStart();
}
/*
Load the Preference Activity if the API LEvel is less than 11 or else load
the PreferenceFragment.
Needed workaround, since unfortunately Google didn't include the
PreferenceFragment in the support library
*/
#Override
public final void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ctx = getApplicationContext();
if (Build.VERSION.SDK_INT < 11)
{
createPreference_Activity();
}
else
{
createPreference_Fragment();
}
}
#Override
protected void onPause()
{
// Unregister OnSharedPreferenceChangeListener
PreferenceManager.getDefaultSharedPreferences(ctx).
unregisterOnSharedPreferenceChangeListener(this);
// Call the base method
super.onPause();
}
#Override
protected void onResume()
{
// Register OnSharedPreferenceChangeListener
PreferenceManager.getDefaultSharedPreferences(ctx).
registerOnSharedPreferenceChangeListener(this);
// Fire the base method
super.onResume();
}
/* ------------------------------ Methods ------------------------------- */
#SuppressWarnings("deprecation")
private final void createPreference_Activity()
{
// Set the Activity layout
addPreferencesFromResource(R.xml.prefs);
// Get the PreferenceScreen ListView
final ListView lvw = getListView();
// Set the horizontal separator
lvw.setDivider(getResources().getDrawable(R.drawable.list_divider));
lvw.setDividerHeight((1));
// Set the statelist selector
lvw.setSelector(R.drawable.list_item_colors);
// Remove the top and bottom fadings
lvw.setVerticalFadingEdgeEnabled(false);
}
#SuppressLint("NewApi")
private final void createPreference_Fragment()
{
// Create the fragment.
getFragmentManager().beginTransaction().replace
(android.R.id.content, new FRG_Prefs()).commit();
getFragmentManager().executePendingTransactions();
}
}
/src/FRG_Prefs
package com.dergolem.abc;
/* ---------------------------------- Imports ------------------------------- */
import android.annotation.SuppressLint;
import android.graphics.PixelFormat;
import android.preference.PreferenceFragment;
import android.view.View;
import android.widget.ListView;
#SuppressLint("NewApi")
public final class FRG_Prefs
extends PreferenceFragment
{
/* ----------------------------- Overrides ------------------------------ */
#Override
public final void onResume()
{
super.onResume();
addPreferencesFromResource(R.xml.prefs);
init();
}
#Override
public final void onStop()
{
super.onStop();
// Kill the prefence screen, so that it won't be recreated DUPLICATE.
// HORRIBLE, but it's the only way to avoid the PreferenceScreen copycat.
getActivity().finish();
}
/* ------------------------------ Methods ------------------------------- */
private final void init()
{
final View v = getView();
v.setPadding(paddingSize, 0, paddingSize, 0);
// Get the PreferenceScreen ListView
final ListView lvw = (ListView) v.findViewById(android.R.id.list);
// Set the horizontal separator
lvw.setDivider(getResources().getDrawable(R.drawable.list_divider));
lvw.setDividerHeight((1));
// Set the state selector
lvw.setSelector(R.drawable.list_item_colors);
// Remove top and bottom fading
lvw.setVerticalFadingEdgeEnabled(false);
}
}
To show my Preferences:
startActivity(new Intent(ctx, ACT_Prefs.class));
ctx is defined as
Context ctx = getApplicationContext();
since I use it a lot, I define it once and for all.
[EDIT]
By request, I could add a method to make a Fake PreferenceScreen.

The answer above is to difficult to implement, so I've designed my own version.
The layout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical">
<include layout="#layout/toolbar"/> <!-- This is a custom toolbar (or actionbar), and not necessary -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/toolbar"
android:paddingRight="?android:attr/scrollbarSize">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#layout/toolbar"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/category_battery"
android:id="#+id/category_misc"
android:layout_marginLeft="#dimen/activity_settings_header_margin" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="2dp"
android:id="#+id/divider"
android:layout_marginLeft="#dimen/activity_settings_margin"
android:layout_below="#+id/category_misc"
android:contentDescription="divider"
android:scaleType="matrix"
android:background="#android:drawable/divider_horizontal_bright"
android:src="#android:drawable/divider_horizontal_bright" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/activity_settings_margin">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/category_calibration"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/category_subjects"
android:layout_marginLeft="#dimen/activity_settings_header_margin"
android:layout_below="#+id/batteryChargeState" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="2dp"
android:id="#+id/divider2"
android:layout_marginLeft="#dimen/activity_settings_margin"
android:layout_below="#+id/category_subjects"
android:contentDescription="divider"
android:scaleType="matrix"
android:background="#android:drawable/divider_horizontal_bright"
android:src="#android:drawable/divider_horizontal_bright" />
<LinearLayout android:layout_width="match_parent"
android:layout_below="#+id/category_subjects"
android:layout_centerVertical="true"
android:layout_height="match_parent"
android:padding="#dimen/activity_settings_margin"
android:orientation="vertical"
android:id="#+id/nextLayout">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
Toolbar xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
Dimens xml:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="activity_settings_margin">24dp</dimen>
<dimen name="activity_settings_header_margin">18dp</dimen>
</resources>
Colors xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="orange">#FDA432</color>
<color name="orange_dark">#ffd17731</color>
</resources>
Just use the your way to store the Preferences. I've created a custom preference class that contains private keys so I can't post the code here without breaking it.
The advantage of using a custom layout like this is that you can add your own toolbar with this line as the first element of the first RelativeLayout.
To use the custom toolbar use this piece of code in your onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle(); // This is for the title when you use a drawer
mToolbar = (Toolbar) findViewById(R.id.toolbar); // This finds the toolbar you've specified using the <include> in the xml
setSupportActionBar(mToolbar); // This sets the toolbar to be used
mToolbar.setBackgroundColor(getResources().getColor(R.color.orange)); // This sets the color of the toolbar
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(getResources().getColor(R.color.orange_dark)); // This sets the color of the navigation bar to a darker orange as used for the toolbar, only when this is supported!
}
mToolbar.setNavigationIcon(R.mipmap.ic_launcher); // This makes the icon clickable, to open and close a drawer if you have one
}

Related

TextView gets overlaped(instead of Overwriting)in a Preference Screen

I am trying to build a preference screen which consists of a Layout with some TextViews and EditText Preference.
When I use textView.setText(inputString) in the Activity, it does not overwrite the existing TextView, instead it creates a new copy of TextView with inputString as text and it overlaps with the existing TextView. Is there any way that the setText overwrites the existing TextView itself.
This is the XML Layout with the TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#color/blue"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"
></ListView>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:textColor="#color/pseudoBlack"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:layout_marginTop="100dp"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:textColor="#color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginStart="100dp" />
<TextView
android:text="TextView"
android:textColor="#color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:id="#+id/textView11"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginStart="21dp" />
</RelativeLayout>
And this is the Preference Screen I am using.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
>
<Preference android:layout="#layout/inner" android:key="innerkey"
/>
<Preference android:key="mskey"
android:title="show"
/>
<EditTextPreference android:key="editpref"
android:title="Add"
/>
</PreferenceScreen>
This is the Activity Class in which I am trying to do setText().
package com.example.asus.layoutcheck;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.widget.TextView;
public class SecondaryActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inner);
TextView txt1 = (TextView)findViewById(R.id.textView2);
TextView txt2 = (TextView)findViewById(R.id.textView5);
TextView txt3 = (TextView)findViewById(R.id.textView11);
txt1.setText("First Text");
txt2.setText("Second Text");
txt3.setText("Third Text");
final String message = "This is the message" ;
addPreferencesFromResource(R.xml.pref);
final Preference msg = findPreference("mskey");
msg.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
msg.setSummary(message);
return true;
}
});
Preference edit = findPreference("editpref");
}
}
I have added the screenshot of the result.
How can I overcome this Issue?Thanks in advance
replace setContentView(R.layout.inner); with addPreferencesFromResource(//your preferenceScreen);
I've encountered this similar issue, and it happens with other view as well like a TextureView inside a FrameLayout. I resized my TextureView smaller and I can still see the previous TextureView in the back flickering.
What made it work is funny -> I just put a black background color (probably any color) to my layout (LinearLayout) and it is working fine now.
I added: android:background="#000000"
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical"
android:visibility="visible"
tools:context=".CameraActivity">
It seems the background is not cleared, if it doesn't know what color to clear it with.

Android TextView doesn't show affected text

Ok so... I've been trying to affect text to TextView (because it's meant to show text, right?)
But for some unknown to me reason it refuses to print whatever I put into it.
My application has two activities, the MainAcivity, or a welcoming screen, per se, and a GameActivity (the project is a Domino game).
I create my previously written java objects at the beginning of the GameActivity class, trying then to show player's name within a TextView.
Player player1 = new Player("Hulk");
creates a player with name Hulk, as you can imagine. I have a method within Player.java to return player's name:
player1.getName()
returns string "Hulk"
I then try to set my TextView's text to hulk, first off by creating a handle to it with:
TextView p1v = (TextView) findViewById(R.textviews.p1view);
then doing the following:
p1v.setText("Player 1 : " + player1.toString());
the p1view is defined as follows within game.xml layout:
<LinearLayout
...
<LinearLayout
...
<TextView
android:id="#+textviews/p1view"
android:text="Board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
I tried p1view.textAppend, tried using android:editable="true". Nothing seems to work.
Funnily enough though, when I try doing the same thing, within MainActivity, it works, as so:
public class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
with the TextView defined within main.xml as follows:
<TextView
android:id="#+textviews/mainview_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Following are the full codes of the files, just to avoid answers like: Give us the full code D:
GameActivity.java:
package domino.asd;
import LI260.*; import android.app.Activity; import android.os.Bundle;
import android.widget.TextView;
public class GameActivity extends Activity {
private Player player1 = new Player("HULK");
private Player player2 = new Player("CPU");
private Pioche bag = new Pioche();
private Plateau board = new Plateau();
private Game game1 = new Game(bag, board, player1, player2);
TextView p1name = null;
TextView p2name = null;
/**
* Called when the activity is first created.
*
* #param savedInstanceState
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
TextView tv = (TextView) findViewById(R.textviews.p1view);
tv.setText(player1.getName());
} }
game.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"
android:weightSum="10"
>
<!-- Played layout -->
<LinearLayout
android:id="#+drawable/gameview_Scores"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_gravity="top"
android:layout_weight="2"
android:layout_marginBottom="10px"
android:orientation="horizontal"
android:background="#ff0000"
android:gravity="center"
>
<Button
android:id="#+buttons/exitButton"
android:text="Exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="#+buttons/drawButton"
android:text="Draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+buttons/scoresButton"
android:text="Show scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- Board layout -->
<LinearLayout
android:id="#+textviews/gameview_Table"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_gravity="center"
android:layout_weight="6"
android:layout_marginBottom="10px"
android:background="#ffff00"
>
<TextView
android:id="#+textviews/p1view"
android:text="Board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- At hand layout -->
<LinearLayout
android:id="#+drawable/gameview_Playable"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="2"
android:layout_gravity="bottom"
android:background="#ff00ff"
>
<TextView
android:id="#+textviews/gameview_AtHandText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
package domino.asd;
import LI260.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Player p1 = new Player("Hulk");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
}
public void btnClick_Name(View view) {
if (view.getId() == R.buttons.B_EnterName) {
EditText playersname = (EditText) findViewById(R.string.playerNameInput);
Toast.makeText(this, "Your name: " + playersname.getText().toString(), Toast.LENGTH_SHORT).show();
}
setContentView(R.layout.game);
}
}
and lastly 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"
android:gravity="center"
>
<EditText
android:id="#+string/playerNameInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="1"
/>
<TextView
android:id="#+textviews/mainview_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+buttons/B_EnterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Save name and start game."
android:onClick="btnClick_Name"
/>
</LinearLayout>
When you are first loading the activity, you are calling this code:
setContentView(R.layout.main);
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
Yet when the user click's the button you are only calling this code:
setContentView(R.layout.game);
Because you have reset the main content view the child view mainview_player has been destroyed and replaced with a new one when inflated from R.layout.game.
You would need to call this code again to find the new view in the game layout and populate it with your player name:
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
You would be better to look into how activities relate to one another and start a new activity for R.layout.game

NullPointerException on Extending a Custom Activity

Have a DashBoardActivity that extends a custom MainActivity but i get NullPointerException when trying to access certain views from the DashBoardActivity. Any idea why this could be happening or something am not seeing here?. Thanks
MainActivity:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_layout);
linearlayout = (LinearLayout)findViewById(R.id.footer_container_id);
actionlayout = (LinearLayout)findViewById(R.id.actionbar_container_id);
mainboard = (ImageView)findViewById(R.id.image_dashboard_id);
dashboard = (ImageView)findViewById(R.id.image_comparison_id);
graph = (ImageView)findViewById(R.id.image_graph_id);
contact = (ImageView)findViewById(R.id.image_contact_id);
actionbar = getSupportActionBar();
actionbarView = getLayoutInflater().inflate(R.layout.actionbar, null);
actionbar.setCustomView(actionbarView, new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
public void setSelectedDashBoard(){
mainboard.setSelected(true);
}
DashBoardActivity:
public class DashBoardActivity extends MainActivity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
int orientation = getResources().getConfiguration().orientation;
sessionmanager = new SessionManager(this);
prefs = getSharedPreferences(PREFS_NAME,0);
String cached_username = prefs.getString(USERNAME, "");
mainboard.setSelected(true); // NullPointerException here.
dashboard_layout.xml:
<?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:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:id="#+id/footer_container_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<include layout="#layout/footer"></include>
</LinearLayout>
<FrameLayout
android:id="#+id/dashboard_fragment_container_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/footer_container_id"
android:layout_alignParentTop="true">
</FrameLayout>
</RelativeLayout>
part of footer.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_footer_id" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentBottom="true"
android:background="#292929" android:orientation="horizontal">
<LinearLayout android:id="#+id/ytd_container_id"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:orientation="vertical">
<ImageView android:id="#+id/image_dashboard_id"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:src="#drawable/footer_icons_ytd" />
<TextView android:id="#+id/ytd_text_id" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_gravity="center"
android:layout_marginBottom="5dp" android:layout_marginLeft="5dp"
android:clickable="false" android:paddingLeft="10dp" android:text="Year to Date"
android:textColor="#FFFFFF" android:textSize="10sp">
</TextView>
"
</LinearLayout>
Update
if i use the method setSelectedDashBoard() instead, which i just added from the MainActivity class, i don't get the NullPointerException. don't understand it..
dashboard = (ImageView)findViewById(R.id.image_comparison_id);
the only reason for dashboard to be null is R.id.image_comparison_id does not belongs to R.layout.dashboard_layout
It looks like a typical error of subclass member variable obscuration. You should check if you re-declared the variable dashboard in DashBoardActivity. The subclass is just substituting the inherited variable with a local one which is not initialised.
It is the character " , unless it is a typo.
</TextView>
**"**
</LinearLayout>

Could not click on the buttons in SlidingDrawer

I have a problem with SlidingDrawer. All buttons in it could not be click.
Here is my xml file for SlidingDrawer:
<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="110dp"
android:content="#+id/content"
android:handle="#+id/handle">
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="#string/menu" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center"
>
<Button
android:id="#+id/btn_video"
style="#style/button_menu_bottom"
android:drawableTop="#drawable/mnu_video"
android:text="#string/video" />
...
...
</LinearLayout>
</SlidingDrawer>
Here is my java file extended from SlidingDrawer
package com.example;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;
import com.example.R;
public class BottomMenuBar extends SlidingDrawer {
private SlidingDrawer mSlidingDrawer;
private Button mButtonSlidingDrawer;
private LinearLayout mLayoutContent;
private Button mButtonVideo;
private Button mButtonPhoto;
private Button mButtonChat;
private Button mButtonSetting;
public BottomMenuBar2(Context context, AttributeSet attrs) {
super(context, attrs);
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(infService);
li.inflate(R.layout.bar_bottom_menu, this, true);
mSlidingDrawer=(SlidingDrawer)findViewById(R.id.sld_bottom_menu);
mButtonSlidingDrawer=(Button)findViewById(R.id.handle);
mLayoutContent=(LinearLayout)findViewById(R.id.content);
mButtonVideo=(Button)findViewById(R.id.btn_video);
mButtonSetting=(Button)findViewById(R.id.btn_setting);
}
public Button getmButtonSlidingDrawer() {
return mButtonSlidingDrawer;
}
public void setmButtonSlidingDrawer(Button mButtonSlidingDrawer) {
this.mButtonSlidingDrawer = mButtonSlidingDrawer;
}
...
Setter & Getter methods
...
}
Here is my main.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:background="#drawable/bg_main"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >
...
Any content
...
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.example.BottomMenuBar
android:id="#+id/sld_bottom_menu"
android:layout_width="wrap_content"
android:layout_height="110dp"
android:handle="#+id/handle"
android:content="#+id/content"
/>
</LinearLayout>
</LinearLayout>
For my buttons in sliding drawers, I tell each button what method to use in the xml with android:onClick.
<Button
android:id="#+id/sortbutton"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:onClick="SortClickHandler"
android:text="#string/sd_sortmethod_button"
android:textSize="12dp" />
And then define the method in whatever activity is appropriate:
public void SortClickHandler(View v) {
// Do stuff here
}
EDIT
I think I found your issue. From the developer docs here it says, and I quote:
SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer
should only be used inside of a FrameLayout or a RelativeLayout for instance.
So, I think if you change your layout to a RelativeLayout you will find that your buttons will work. I just double-checked all of my uses of a sliding drawer and in all cases the root element of the view containing it is a RelativeLayout.
I don't understand why you're extending SlidingDrawer.
You should look up the documentation here.
<SlidingDrawer
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:handle="#+id/handle"
android:content="#+id/content">
<ImageView
android:id="#id/handle"
android:layout_width="88dip"
android:layout_height="44dip" />
<FrameView
android:id="#id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
[INSERT VIEWS HERE]
<FrameView>
</SlidingDrawer>
In your Activity you don't even technically need to gain access to the SlidingDrawer object (The sliding functionality is already in place as standard, it simply contains the the view's in content, you don't need to interfere).
From your Activity gain reference to the view's you add in [INSERT VIEWS HERE]. You can then do setOnClickListener() on each of these buttons.

Android sliding menu to change content on page can this be done?

The main page has a horizontalScrollView across the top and a number of TextView text fields within it. I would like to be able to scroll and click on any on the TextView text and it change the RelativeLayout to match the selection made in the HorizontalScrollView.
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"
>
<HorizontalScrollView android:id="#+id/horizontalScrollView1" android:layout_height="wrap_content" android:scrollbars="none" android:layout_width="wrap_content">
<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent" android:id="#+id/linearLayout1">
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTOne" android:text="tOne"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTTwo" android:text="tTwo"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTThree" android:text="tThree"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFour" android:text="tFour"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFive" android:text="tFive"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSix" android:text="tSix"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSeven" android:text="tSeven"></TextView>
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/relativeLayout1"></RelativeLayout>
</LinearLayout>
I have a second layout page called tone which would be linked to the menu option tOne in the HorizontalScrollView.
tone.xml
<?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_height="match_parent">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is tOne"></TextView>
</LinearLayout>
The main activity will flag if one of the first 4 options are selected by writing to the LogCat. Is it possible to display the tone.xml within the relativelayout if the user clicks the tOne option from the HorizontalScrollView? Then if the user selects tTwo display ttwo.xml in the RelativeLayoutView etc?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SlideMenu extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView tOne, tTwo, tThree, tFour, tFive, tSix, tSeven, tEight, tNine, tTen;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout vSpace = (RelativeLayout)findViewById(R.id.relativeLayout1);
tOne = (TextView)findViewById(R.id.EditTOne);
tTwo = (TextView)findViewById(R.id.EditTTwo);
tThree = (TextView)findViewById(R.id.EditTThree);
tFour = (TextView)findViewById(R.id.EditTFour);
tFive = (TextView)findViewById(R.id.EditTFive);
tSix = (TextView)findViewById(R.id.EditTSix);
tSeven = (TextView)findViewById(R.id.EditTSeven);
tOne.setOnClickListener(this);
tTwo.setOnClickListener(this);
tThree.setOnClickListener(this);
tFour.setOnClickListener(this);
tFive.setOnClickListener(this);
tSix.setOnClickListener(this);
tSeven.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
//--------------
case R.id.EditTOne:
System.out.println("Text One pressed");
break;
case R.id.EditTTwo:
System.out.println("Text Two pressed");
break;
case R.id.EditTThree:
System.out.println("Text Three pressed");
break;
case R.id.EditTFour:
System.out.println("Text Four pressed");
break;
}
}
}
I believe it is fairly simple. I added
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.tone, vSpace);
after you print "Text One Pressed". Be sure to make vSpace a field first.
To do this with all of them, call
vSpace.removeAllViews()
before you inflate each view.

Categories

Resources