How Can I set Actionbar, Title and BackButton in PreferenceActivity like AppCompactActivity...? - android-actionbar

this is my Setting class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
Please help me guys how can set ACTIONBAR, TITLE and BACLBUTTON like AppcompactActivity
This is my main Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
boolean first =settings.getBoolean("first",false);
String rings=settings.getString("ringtone","Default Rings");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent intent= new Intent(MainActivity.this,SettingClass.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}

Related

ActionBar back button crash my app

I have problem with my school project when I want to back to my CurentCourseActivity using back button form action bar I have NE
Attempt to invoke virtual method 'java.util.List `com.example.pingu.mylanguages.Language.getList()' on a null object reference at
com.example.pingu.mylanguages.CurentCourseActivity.onCreate(CurentCourseActivity.java:37)`
While I use normal back button problem is not appear.
When we choose Lesson from GridView that makes a new activity. When we choose the back button from ActionBar then I have NE.
CourentCourseActivity:
public class CurentCourseActivity extends AppCompatActivity {
private Language language;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("s","onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_curent_course);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState!=null){
language = (Language) savedInstanceState.getSerializable("Lang");
Log.d("xx","Coś ma");
}
if (getIntent().getExtras() != null) {
language = (Language) getIntent().getSerializableExtra("Lang"); //Obtaining data
}
GridView grid = (GridView) findViewById(R.id.grid);
CurrentCourseAdapter adapter = new CurrentCourseAdapter(this, R.layout.grid_item_curent_course, language.getList());
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(CurentCourseActivity.this,LessonActivity.class);
intent.putExtra("Lesson",(Lesson)language.getList().get(i));
startActivity(intent);
}
});
getSupportActionBar().setTitle(language.getName());
}
}
LessonActivity:
public class LessonActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(getIntent().getExtras()!=null){
Lesson lesson = (Lesson) getIntent().getSerializableExtra("Lesson");
getSupportActionBar().setTitle(lesson.getNamel());
}
}
}
The answer is simplest then I expect. So I need to overide this button and do it as normal back button because ActrionBar back button destroy parent activity.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

How to set image in list view by using string

I have two activities namely mainactivity and main2activity. Mainactivity has three buttons and main2activity has list view.
When I click button string is passed to main2activity and is displayed in main2activity.
So now I want also image in listview. corresponding image for button selected. Below are my codes.
MainActivity
public class MainActivity extends AppCompatActivity {
EditText editText;
Button addButton;
String text1 ="Item 1";
String text2 ="Item 2";
String text3 ="Item 3";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("text",text1);
startActivity(intent);
}
});
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("text",text2);
startActivity(intent);
}
});
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("text",text3);
startActivity(intent);
}
});
}
Main2activity
public class Main2Activity extends AppCompatActivity {
ListView listView;
ArrayAdapter<String> adapter;
#Override
protected void onRestart() {
super.onRestart();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Global.list1);
listView.setAdapter(adapter);
if (Global.list1.size() < 1) {
Global.list1 = new ArrayList<>();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent1 = getIntent();
listView = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Global.list1);
listView.setAdapter(adapter);
String havestring = intent1.getStringExtra("text");
if (havestring != null) {
Global.list1.add(havestring);
adapter.notifyDataSetChanged();
}
Button backbtn = (Button) findViewById(R.id.backbtn);
backbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageview"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
</LinearLayout>
So how to set custom adapter that will add corresponding image with string added.
Can it be done using if else statement in custom adapter???
Please do help me
for same.
Can anyone help me out???

Populating a spinner used in more than two activities

I have an action bar spinner and two Activities in my app. The spinner, which is populated from a database, needs to be visible in both my activities. How should I generate it without duplicating my code? Should I create a static method or something else?
private NotesDbAdapter mDbHelper;
private List<String> listUniqueCat;
int selectedPos;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_joke_details);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
//this method returns list of strings from a database
listUniqueCat = mDbHelper.getUniqueCategories();
// create an array adapter to popluate dropdown list
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getBaseContext(),
android.R.layout.simple_spinner_dropdown_item, listUniqueCat);
// enable dropdown list naaavigation in action bar
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// defining navigation listiner
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId)
{
selectedPos = getActionBar().getSelectedNavigationIndex();
Toast.makeText(getBaseContext(), "selected index is "+ selectedPos ,
Toast.LENGTH_LONG).show();
return false;
}
};
// setting dropdown items and item navigation listener for action bar
getActionBar().setListNavigationCallbacks(adapter, navigationListener);
}
I think you need to create one BaseActivity with tabs like
abstract public class BaseActivity extends FragmentActivity {
}
and all other activities extends BaseActivity
small example for you
BaseActivity.java
abstract public class BaseActivity extends Activity implements TabListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
ActionBar actionBar = getActionBar();
// add tabs to actionbar
actionBar.addTab(actionBar.newTab().setText("TAB 1")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("TAB 2")
.setTabListener(this));
}
}
FirstActivity.java
public class FirstActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Toast.makeText(getApplicationContext(),
tab.getText() + " selected in FirstActivity",
Toast.LENGTH_SHORT).show();
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
SecondActivity.java
public class SecondActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Toast.makeText(getApplicationContext(),
tab.getText() + " selected in SectondActivity",
Toast.LENGTH_SHORT).show();
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}

does not show simple Action bar in android app

I am trying to make an action bar in my app. I have already hide the default action bar but it is not showing my action bar.
I am using code from :-
http://developer.android.com/guide/topics/ui/actionbar.html
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_category, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
here is code for menu:-

how to refresh the linear layout view after deleting an element

I have a simple app, in one activity I take name and date of birth. I store it in the database. and in the main activity I have linearlayout which will show all the names.
When I click on any of the name in the main activity, it should delete that name from the database and also refresh the view.
I am able to delete the entry from database, but my linear layout view is not being updated. Can some one pls help.
public class child extends Activity {
private Intent intent;
private LinearLayout layout;
private LayoutInflater linflater;
private int i =0;
private Cursor cr;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.child);
layout = (LinearLayout)findViewById(R.id.layout);
Button addBtn = (Button)findViewById(R.id.AddButton);
Button remBtn = (Button)findViewById(R.id.RemoveButton);
intent = new Intent(this,login.class);
layout = (LinearLayout) findViewById(R.id.mylayout1);
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Check the database if there are any entries available. If available, then
//list them on the main screen
final myDBAdapter mydb = new myDBAdapter(getApplicationContext());
mydb.open();
cr = mydb.GetMyData();
if(cr.getCount()>0)
{
cr.moveToFirst();
for (int i=0;i<cr.getCount();i++)
{
cr.moveToPosition(i);
buildList(cr.getString(1),cr.getString(2));
}
}
//Start the login activity which will return the newly added baby name
addBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivityForResult(intent, 1001);
}
});
//Remove all the entries from Database
remBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(cr.getCount()>0)
{
cr.moveToFirst();
for (int i=0;i<cr.getCount();i++)
{
Toast.makeText(getApplicationContext(), cr.getString(1),
Toast.LENGTH_LONG).show();
mydb.RemoveEntry(cr.getString(1));
cr.moveToPosition(i);
}
}
}
});
mydb.close();
}
private void buildList(final String bname,String bsex)
{
final View customView = linflater.inflate(R.layout.child_view,
null);
TextView tv = (TextView) customView.findViewById(R.id.TextView01);
//tv.setId(i);
tv.setText(bname);
tv.setTextColor(getResources().getColor(R.color.black));
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myDBAdapter mydb = new myDBAdapter(getApplicationContext());
mydb.open();
if (mydb.RemoveEntry(bname)>0)
{
Toast.makeText(getApplicationContext(), "Row deleted",
Toast.LENGTH_LONG).show();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
WHAT IS REQUIRED HERE TO UPDATE THE VIEW???
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
else
{
Toast.makeText(getApplicationContext(), "Row not deleted",
Toast.LENGTH_LONG).show();
}
}
});
layout.addView(customView);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 1001)
{
if(resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
buildList(extras.getString("bname"),extras.getString("bsex"));
}
}
}
}
Hmm I'm also trying to get this to work, Have you tried looking at maybe refreshing the LinearLayout every say 5 seconds using a game loop? This may prove to be useful as it helped me with my problem http://aubykhan.wordpress.com/2010/04/25/android-game-programming-the-game-loop/
You may also be able to call onCreate(Bundle) function again while this is a terrible terrible way to do this it will work.