Extend Navigation Bar into another activity (no fragments) - android

I read some question regarding this but all the answers are about fragments and there is question similar to this one but the answer is incomplete, I want to reuse a set of layout or codes into multiple activities, i created a baseActivity that extends into Activity with the code below.
I also read that you need to put the code in the onCreateOptionMenu but it is still not working. (the code in baseacitivty xml is working, and homepage xml is working but does not show the navigation_layout)
public class BaseActivity extends Activity {
private ImageButton ibButtonHome;
private ImageButton ibButtonFavorite;
private ImageButton ibButtonRandomize;
private ImageButton ibButtonHistory;
private ImageButton ibButtonLogOut;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_layout);
}
View.OnClickListener Navigation = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent();
if (v.equals(ibButtonHome)) {
i.setClass(getBaseContext(), HomePage.class);
} else if (v.equals(ibButtonFavorite)) {
i.setClass(getBaseContext(), Favorite.class);
} else if (v.equals(ibButtonHome)) {
i.setClass(getBaseContext(), HomePage.class);
} else if (v.equals(ibButtonRandomize)) {
i.setClass(getBaseContext(), Randomize.class);
} else if (v.equals(ibButtonHistory)) {
i.setClass(getBaseContext(), History.class);
} else if (v.equals(ibButtonLogOut)) {
//TODO: something code here to not crash on activity exit??
i.setClass(getBaseContext(), MainActivity.class);
}
startActivity(i);
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
ibButtonHome = (ImageButton) findViewById(R.id.button_Home);
ibButtonFavorite = (ImageButton) findViewById(R.id.button_favorites);
ibButtonRandomize = (ImageButton) findViewById(R.id.button_randomize);
ibButtonHistory = (ImageButton) findViewById(R.id.button_history);
ibButtonLogOut = (ImageButton) findViewById(R.id.button_logout);
ibButtonFavorite.setOnClickListener(Navigation);
ibButtonRandomize.setOnClickListener(Navigation);
ibButtonHome.setOnClickListener(Navigation);
ibButtonHistory.setOnClickListener(Navigation);
ibButtonLogOut.setOnClickListener(Navigation);
getMenuInflater().inflate(R.menu.menu_filter_menus, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Homepage activity
public class HomePage extends BaseActivity {
private CustomAdpaterFoodFeed ExpAdapter;
private ArrayList<FoodFeed> foodFeeds;
private ExpandableListView ExpandList;
//Onclick listener for the Navigation Bar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
ExpandList = (ExpandableListView) findViewById(R.id.evFoodFeed);
//runs the function and returns the data to foodFeeds
foodFeeds = SetStandardGroups();
//Adapter for ExapadableListView
ExpAdapter = new CustomAdpaterFoodFeed(HomePage.this, foodFeeds);
ExpandList.setAdapter(ExpAdapter);
}
// Dummy data method for pictures and comments
public ArrayList<FoodFeed> SetStandardGroups() {
String names[] = {"Geraldine", "Marielle", "Gina", "Bryan",
"Pat", "Eugene", "Shermaine", "Kook"};
String comments[] = {"TasteGood", "Nah", "DONT EAT HERE", "Cameroon",
"Nice place", "chill", "woah Spain", "lalala"};
int Images[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher
};
ArrayList<FoodFeed> list = new ArrayList<FoodFeed>();
ArrayList<Comments> comments_list;
for (int images : Images) {
FoodFeed gru = new FoodFeed();
gru.setIcon(images);
comments_list = new ArrayList<Comments>();
for (int j = 0; j < 4; j++) {
Comments comments1 = new Comments();
comments1.setName(names[j]);
comments1.setComments(comments[j]);
comments_list.add(comments1);
}
gru.setComments(comments_list);
list.add(gru);
}
return list;
}
#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_home_page, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Base activity to extend the navigation drawer in other activities you can follow this link , described well. it is well tested i followed the same :)
http://androiddeveloperdemo.blogspot.in/2014/08/android-navigation-drawer-with-multiple.html
You can get the same action bar in other activities by declaring in AndroidManifest.xml like this
<activity
android:name=".SettingsActivity"
android:label="#string/activity_title"
android:theme="#style/AppTheme" />
For different menu options define a xml file under menu folder in android studio and inflate that file in
onCreateOptionsMenu(Menu) overridden method of your activty

Related

App Constant in application

I created Listview and menu options in android application with App Constants to minimize the number of classes
Ex:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,prgmNameList));
lv.setOnItemClickListener(this);
}
OnClickListener
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position)
{
case 0:
Intent intent = new Intent(this, First.class);
intent.putExtra(AppConstants.MAIN_ACTIVITY_TAG,lv.getItemAtPosition(position).toString());
startActivity(intent);
break;
}
But i need apply app constants to Menu Options too
But i don't know how to do that:
Here is the code for Menu Options
// create action options for options
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_about) {
Intent intent = new Intent(this,Extras.class);
startActivity(intent);
}
if (id == R.id.action_advertise) {
return true;
}
if (id == R.id.action_contact) {
return true;
}
if (id == R.id.action_help) {
return true;
}
return super.onOptionsItemSelected(item);
}
I want to do same as listview but for menu options.
Please help me solve this problem.
Just put your string in string.xml .
<string name="menu_name">Show result</string>
and you directly access it in xml .
<menu 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"
tools:context=".ContainerActivity">
<item
android:id="#+id/menu_action_show"
android:layout_width="wrap_content"
android:icon="#drawable/youricon"
android:title="#string/menu_name"
app:showAsAction="always"/>
You can also access all resources using java code with context :
String menu_name=getString(R.id.menu_name);

Silly errors (beginner)

I have a first activity ("Home"), with two buttons: one is called About and leads to activity About and the second is named List and leads to the activity List.
Manifest.xml should be fine, but I get a load of tiny petty errors I can't fix up by myself, regrettably.
Home.class is the following
Public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button AboutButton = (Button)findViewById(R.id.About);
AboutButton.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View view); {
Intent openAbout = new Intent(Home.this, About.class);
startActivity(openAbout);
}
}
Button ListButton = (Button)findViewById(R.id.List);
ListButton.setOnClickListener(new View.onClickListener());{
#Override
public void onClick(View view); {
Intent openList = new Intent(Home.this, List.class);
startActivity(openList);
}
}
}
#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_home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
while About.class is like this
public class About extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
public void onClick(View view); {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
}
}
#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_about, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and List is like this:
public class List extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View view) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
}
}
#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_list, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I get lot of red light bulbs, saying, for instance that ")" or ";" is expected or (worse) onClickListener cannot be resolved
Last but not least: I copied this code online and I was wondering why after "View" there is a "view"; what does it mean?
I copied your code and I see fails everywhere... let me explain you what's going on ...
HOME CLASS
1.- You have to remove the ")"
2.- You don't have to ";" when you call onClick()
3.- When you are don on your onClick() NOW you have to close it, you missed the ");"
AboutButton.setOnClickListener(new View.OnClickListener()){ //<-- Just remove one
#Override
public void onClick(View view); { //<-- Remove this ";"
Intent openAbout = new Intent(Home.this, About.class);
startActivity(openAbout);
}
}//Here goes ");"
4.- The ListButton has the same issues so just fix it as you will fix the first one.
ABOUT CLASS
1.-On this case you have the setOnClickListener() ok, BUT why you have two onClick(View view)? It's not necessary just remove one of them.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Button ReturnButton = (Button) findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
});
}
LIST CLASS
1.-Well in your List class you have made the same error as the first one... Your onClickListener() it's wrong.
2.-Once again you included an unnecessary ")" on new View.OnClickListener() just remove it,
3.-Another fail that I'm seeing is that you are trying to make an Intent but you are refering that you are on About.this and you are NOT. You are on List class so you have ot put List.this because the first parameter refers :
A Context as its first parameter (this is used because the Activity class is a subclass of Context)
More information about Intents
4.- You need to close again the setOnClickListener()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener()){ //<-- remove one ")"
#Override
public void onClick(View view) {
Intent returnhome = new Intent(About.this, Home.class); //<-- Remove About.this and put List.this
startActivity(returnhome);
}
}//<-- Close the setOnClickListener() with ");"
}
It's okay guy, this is your first question and I'll answer it, but NOW as I've made the favor to take my time and explain to you what was wrong on your code take your time to :
How do I as a question on StackOverflow
Learn some Android basics
And the most IMPORTANT THING
DO NOT COPY PASTE AN INTERNET CODE if you don't know the basics, I mean you can copy paste the code, but you'll face with this problem every time you do this, so first of all read the tutorial, make an examples, and you'll improve every day.

passing value from mainactivity to a listview

Hi everyone I am currently developing an android app, I am having some confusions in the code, I have a main activity in which I store some array values.
I have two xml files(one mainactivity.xml and other the other one is listview.xml)
In main activity.xml there are four switches, when I click any particular switch it should take me to the listview.xml, with a corresponding array displayed on list view by list view adapter. The code is as follows
public class MainActivity extends ActionBarActivity {
ListView l;
Button chem = (Button) findViewById(R.id.button4);
public String[] contentc = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l= (ListView) findViewById(R.id.listview);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_element, contentp);
chem.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//DO SOMETHING! {RUN SOME FUNCTION ... DO CHECKS... ETC}
setContentView(R.layout.list);
l.setAdapter(adapter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);
In activity1, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message")

Android layouts: create board, centered and automatic sized

I spent a week trying to make this, but with no result.
I want to make something like what's in the image.
I want to make table of buttons (the user chooses 5x5, 10x10, or
something else)
The table should be centered horizontally and vertically
Each button in the table cell should be sized relatively to the
device screen
Each button should be a rectangle.
EDIT:
Here my code:
public class MainActivity extends ActionBarActivity {
private int game_counter=1;
private int nof_columns = 10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GridView table_layout = new GridView(this);
table_layout.setNumColumns(this.nof_columns);
int button_size=5;
for(int rows=1; rows<=this.nof_columns; rows++) {
TableRow rowT = new TableRow(this);
for(int columns=1; columns<=this.nof_columns; columns++){
Button bn = new Button(this);
bn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onclick_function(v);
}
});
bn.setId(100 + rows * this.nof_columns + columns);
bn.setWidth(button_size);
bn.setHeight(button_size);
bn.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
MyTags tag = new MyTags();
bn.setTag(tag);
rowT.addView(bn);
}
//rowT.setPadding(-5,-5,-5,-5);
rowT.setMinimumHeight(button_size);
rowT.setMinimumWidth(button_size);
table_layout.addView(rowT);
//table_layout.setPadding(-70,-70,-70,-70);
}
setContentView(table_layout);
}
#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) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I found the solution.
It's mentioned in Defining a percentage width for a LinearLayout?
Later I'll post here my code with the board.
Thanks!

error whilst passing values between activities

I have an activity which has an edit text which becomes visible when a button is clicked. I fill the edit text up and click another button. On clicking this button the edit text content must be sent to another activity.The first activity takes the edit text and queries a list of data from my Parse database and shows it in a ListView in the Second Activity.But whenever i click the first button(after entering the string) the app crashes.This is the first activity
public class MainActivity extends ActionBarActivity {
String name;
EditText search;
Button g;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpSpinners();
Parse.initialize(this, "AAh5US7zhbYyFBexsv07cjo34ZZiB7KNe9SuTv7e",
"eKUG1pYaV50hVyDC9d4qZc4qf1dCtOTqnX92eGJV");
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation();
search = (EditText) findViewById(R.id.search);
g = (Button) findViewById(R.id.Go);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void byName(View v) {
search.setVisibility(View.VISIBLE);
search.requestFocus();
g.setVisibility(View.VISIBLE);
}
public void Go(View v) {
name = search.getText().toString();
final Intent i;
i = new Intent(MainActivity.this, ResterauntList1.class);
i.putExtra("restrauntName", name);
startActivity(i);
}
}
In the above byName is the onClick for making the EditText visible, and Go is the onClick for getting my EditText string and passing it to the next activity. The second activity is below
public class ResterauntList1 extends Activity {
String rValue;
ArrayAdapter<String> adapter;
ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resteraunt_list1);
Bundle bdl = getIntent().getExtras();
rValue = bdl.getString("restrauntName");
setContentView(R.layout.activity_resteraunt_list);
populateList(rValue, "name");
}
private void populateList(final String Value, final String Key) {
ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {
#Override
#SuppressWarnings({ "unchecked", "rawtypes" })
public ParseQuery create() {
ParseQuery query = new ParseQuery("resdb");
query.whereEqualTo(Key, Value);
return query;
}
};
ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(
this, factory);
adapter.setTextKey("name");
adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
#Override
public void onLoading() {
mProgressDialog = new ProgressDialog(ResterauntList1.this);
mProgressDialog.setTitle("Searching for " + Value);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
public void onLoaded(List<ParseObject> objects, Exception e) {
mProgressDialog.dismiss();
}
});
final ListView listView = (ListView) findViewById(R.id.restListView1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ParseObject object = (ParseObject) listView
.getItemAtPosition(position);
String Id = object.getObjectId();
Intent i = new Intent(getApplicationContext(),
SingleRestraunt.class);
i.putExtra("restId", Id);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.resteraunt_list1, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The error as stated above occurs when I click the Go button.The error is
09-02 14:58:46.443: E/AndroidRuntime(3061): Process: com.example.gastronomaapp, PID: 3061
09-02 14:58:46.443: E/AndroidRuntime(3061): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gastronomaapp/com.example.gastronomaapp.ResterauntList1}: java.lang.NullPointerException
Any idea where I am making a mistake? The funniest thing almost the same code has worked in another part of my app. absolutely clueless whats wrong.
Bundle bdl = getIntent().getExtras();
rValue = bdl.getString("restrauntName");
change to
rValue = getIntent().getStringExtra("restrauntName");
You put the string directly on the intent, not packaged in a bundle.

Categories

Resources