Android Same Button and Listview in All Activities - android

I want to ask a question about optimizing my code. I am working on a project and i have code working for onItemClick listner and button click handler the problem is that i have 9 different activities and i have to copy and paste the same code in all my activities the issue i am having is too much of same code copy pasted into each activity
The Highlighted Section of the sliding menu are same in all activies all i have to do is register their click listners again and again to make them working and copy the same code in all activities. i want it to be generic i.e. code written in one place should be working for all the activities.
This app is in final launch mode and i cannot shift to sliding menu using navigation drawer that was the main reason i used this approach and the top menu also has different buttons which clicks need to be managed dynamically. i tried making this static but it didnt worked.
Thanking You for your time and replies.

That sounds like the perfect use for a fragment. Place the views and the related code in a fragment, and include the fragment in each activity.

What Gabe mentions would be the perfect way to go. However, if you do want to continue with multiple activities, you could create a class extending Activity with all the code for the sliding menu inside it. Then make sure that all other activities extend the new class you created.

visit Android Sliding Menu using Navigation Drawer tutorial for using navigation drawer...
this may help you..

This is the Solution to this Problem
public class SuperActivity extends Activity implements OnClickListener,
OnItemClickListener {
protected static Button btn_logout;
protected static ListView lv_SlidingMenu;
protected static FlyOutContainer rootView;
protected static TextView tv_userName;
protected static TextView tv_memberSince;
protected static ImageView iv_userImage;
protected static ImageView iv_top_home;
protected static TextView tv_top_home;
protected ImageView iv_slidingmenu;
protected static SlidingMenuAdapter slidingMenuAdapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
switch(parent.getId()){
case R.id.list:
switch(position){
case 0:
GeneralDataModel.actionIntent = new Intent(this,
ActivityTheGreatControversy.class);
this.startActivity(GeneralDataModel.actionIntent);
break;
case 1:
break;
case 2:
GeneralDataModel.actionIntent = new Intent(this,
AtlastActivity.class);
this.startActivity(GeneralDataModel.actionIntent);
break;
case 3:
GeneralDataModel.actionIntent = new Intent(this,
MediaActivity.class);
this.startActivity(GeneralDataModel.actionIntent);
break;
case 4:
GeneralDataModel.actionIntent = new Intent(this,
TimeLineActivity.class);
this.startActivity(GeneralDataModel.actionIntent);
break;
case 6:
GeneralDataModel.actionIntent = new Intent(getApplicationContext(),
ActivityNotes.class);
this.startActivity(GeneralDataModel.actionIntent);
GeneralDataModel.actionIntent = null;
break;
case 10:
GeneralDataModel.actionIntent = new Intent(this,
ActivitySettings.class);
this.startActivity(GeneralDataModel.actionIntent);
rootView.toggleMenu();
break;
default:
rootView.toggleMenu();
break;
}
break;
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "CLICKEDiy", Toast.LENGTH_LONG)
.show();
switch (v.getId()) {
case R.id.btn_sliding_logout:
GeneralDataModel.actionIntent = new Intent(getApplicationContext(),
LoginSignup.class);
GeneralDataModel.actionIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(GeneralDataModel.actionIntent);
GeneralDataModel.actionIntent = null;
new SessionManager(this).logoutUser();
this.finish();
break;
case R.id.iv_home_slidingmenu:
rootView.toggleMenu();
break;
}
}
protected void fillSlidingMenu() {
tv_userName.setText(UserInformation.getFirstName() + " "
+ UserInformation.getLastName());
tv_memberSince.setText(UserInformation.getMemberSince());
lv_SlidingMenu.setAdapter(slidingMenuAdapter);
}
}
and derive your all activities from This Class
Then in their onClickListners just simply call super.onclick(v);

Related

I can't make new intent

my problem with new intent , the problem is with this: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.test.test1/com.test.test1.facebook}; have you declared this activity in your AndroidManifest.xml?
`package com.test.test1;
public class SampleActivity extends Activity implements OnItemSelectedListener,
OnItemClickListener, OnRotationFinishedListener, OnCenterClickListener {
public static final String ARG_LAYOUT = "layout";
private TextView selectedTextView;
#Override
protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set content view by passed extra
Bundle extras = getIntent().getExtras();
int layoutId = extras.getInt(ARG_LAYOUT);
setContentView(R.layout.sample_with_background);
// Set listeners
CircleLayout circleMenu = (CircleLayout) findViewById(R.id.main_circle_layout);
circleMenu.setOnItemSelectedListener(this);
circleMenu.setOnItemClickListener(this);
circleMenu.setOnRotationFinishedListener(this);
circleMenu.setOnCenterClickListener(this);
selectedTextView = (TextView) findViewById(R.id.main_selected_textView);
selectedTextView.setText(((CircleImageView) circleMenu
.getSelectedItem()).getName());
}
#Override
public void onItemSelected(View view, String name) {
selectedTextView.setText(name);
switch (view.getId()) {
case R.id.main_calendar_image:
// Handle calendar selection
break;
case R.id.main_cloud_image:
// Handle cloud selection
break;
case R.id.main_facebook_image:
// Handle facebook selection
break;
case R.id.main_key_image:
// Handle key selection
break;
case R.id.main_profile_image:
// Handle profile selection
break;
case R.id.main_tap_image:
// Handle tap selection
break;
}
}
#Override
public void onItemClick(View view, String name) {
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.start_app) + " " + name,
Toast.LENGTH_SHORT).show();
switch (view.getId()) {
case R.id.main_calendar_image:
// Here is my problem i cant start a new intent why ?
Intent myIntent = new Intent(view.getContext(), facebook.class);
startActivityForResult(myIntent, 0);
break;
case R.id.main_cloud_image:
// Handle cloud click
break;
case R.id.main_facebook_image:
// Handle facebook click
break;}
}
#Override
public void onRotationFinished(View view, String name) {
Animation animation = new RotateAnimation(0, 360, view.getWidth() / 2,
view.getHeight() / 2);
animation.setDuration(250);
view.startAnimation(animation);
}
#Override
public void onCenterClick() {
Toast.makeText(getApplicationContext(), R.string.center_click,
Toast.LENGTH_SHORT).show();
}
}
`
change this line in your code.
Intent myIntent = new Intent(SampleActivity.this, facebook.class);
you have declare Facebook activity in your manifestfile.xml
I guess the reason why you can't start the new intent is because you are using lowercase instead of uppercase for that facebook class.
Intent myIntent = new Intent(this, Facebook.class);
I think in your manifest have not facebook activity if have declared it but still can not start intent then you can check R.id.main_calendar_image view really get clicked
Instead of using view.getContext() use the full Activity.this way.
Intent myIntent = new Intent(SampleActivity.this, Facebook.class);
Oh and do ensure that it is declared in your manifest

Second onClick listener in a new activity

I just started to learn Java. I know some C++, but you know, I am just a novice. I have a problem with a button. I a main activity there are 3 buttons with onClick discovered by switch. By clicking on one of the buttons you're redirected to another activity where I need to create a new button.
The code responsible for MainScreen buttons looks like this (and it works):
public class MainScreen extends Activity implements View.OnClickListener {
Button act_2x2, act_3x3, act_4x4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
act_2x2 = (Button) findViewById(R.id.Activity_2x2);
act_3x3 = (Button) findViewById(R.id.Activity_3x3);
act_4x4 = (Button) findViewById(R.id.Activity_4x4);
act_2x2.setOnClickListener(this);
act_3x3.setOnClickListener(this);
act_4x4.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch(view.getId())
{
case R.id.Activity_2x2:
Intent inent1 = new Intent(this, macierz_2x2.class);
startActivity(inent1);
break;
case R.id.Activity_3x3:
Intent inent2 = new Intent(this, macierz_3x3.class);
startActivity(inent2);
break;
case R.id.Activity_4x4:
Intent inent3 = new Intent(this, macierz_4x4.class);
startActivity(inent3);
break;
}
And it is okay, I can normally enter the new activity, for example Activity_2x2.
Here, in 2x2 class I've created a new OnClickListener and when I click on it, nothing happens. I am sitting here for two hours with debugger, it is saying that I don't have permissions, but It is impossible, because it is just a simple button. I am using Android Studio and just don't know how to debug correctly.
Here is the definition:
public class macierz_2x2 extends MainScreen implements View.OnClickListener{
Button b_2x2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2x2);
b_2x2 = (Button) findViewById(R.id.button_2x2);
b_2x2.setOnClickListener(this);
}
public void OnClick(View view) {
what happens after clicking
}
I know, that this problem is somewhere in overriding and extending, but no idea, why the compiller is letting this being compiled.
If someone have any idea, I will be grateful.
ps. I don't need an answer, just a point, what is wrong.
public class macierz_2x2 extends MainScreen implements View.OnClickListener{
MainScreen already implements View.OnClickListener. Remove it from the definition of your class.
public class macierz_2x2 extends MainScreen {
is enough. You can override onClick on your macierz_2x2 activity
#Override
public void onClick(View view) {
switch(view.getId()) {
R.id.button_2x2:
// do something
break;
default:
super.onClick(view);
break;
}
}

Android Buttons wont activate

I know this has been asked a million times but none have led me to solving my problem. The onclicklistener will not activate the code for any of the buttons. Here are the different sections that apply to the five buttons.
Button btnGuysMax;
Button btnGuysMedium;
Button btnEven;
Button btnGirlsMedium;
Button btnGirlsMax;
....
private void init()
{
datasource = new BarsDataSource(this);
datasource.open();
Intent intent = getIntent();
long id = intent.getLongExtra("bar_id",0);
bar = datasource.getBarById(id);
title = (TextView)findViewById(R.id.title);
btnGuysMax = (Button)findViewById(R.id.btnGuysMax);
btnGuysMedium = (Button)findViewById(R.id.btnGuysMedium);
btnEven = (Button)findViewById(R.id.btnEven);
btnGirlsMedium = (Button)findViewById(R.id.btnGirlsMedium);
btnGirlsMax = (Button)findViewById(R.id.btnGirlsMax);
......
btnGuysMax.setOnClickListener(this);
btnGuysMedium.setOnClickListener(this);
btnEven.setOnClickListener(this);
btnGirlsMedium.setOnClickListener(this);
btnGirlsMax.setOnClickListener(this);
.....
#Override
public void onClick(View view)
{
//resetButtons();
switch (view.getId()) {
case R.id.btnGuysMax:
//bar.setSexRatio(-2);
//btnGuysMax.setBackgroundColor(guysMaxColor);
Toast.makeText(this,"Max clicked!",Toast.LENGTH_LONG);
break;
case R.id.btnGuysMedium:
bar.setSexRatio(-1);
Toast.makeText(this,"Medium clicked!",Toast.LENGTH_LONG);
//btnGuysMedium.setBackgroundColor(guysMediumColor);
break;
case R.id.btnEven:
bar.setSexRatio(0);
//Toast.makeText(this,"Medium clicked!",Toast.LENGTH_LONG);
break;
case R.id.btnGirlsMedium:
bar.setSexRatio(1);
//btnGirlsMedium.setBackgroundColor(girlsMediumColor);
break;
case R.id.btnGirlsMax:
bar.setSexRatio(2);
break;
.....
To display a toast you need to call show method.
Try:
Toast.makeText(this,"message",Toast.LENGTH_LONG).show();
try doing:
#Override
public void onClickListener(View view)
{
instead of:
#Override
public void onClick(View view)
{

using switch case statement for spinner to display images in android programming

I have a spinner which has a list of attractions. I want to use switch case statements to change the image displayed depending on which attraction the user selects.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.attractions_layout);
ArrayAdapter<String> attractionsAdapter = new ArrayAdapter<String> (Attractions.this, android.R.layout.simple_spinner_item, attractionEntries);
attractionsSpinner = (Spinner) findViewById (R.id.spinnerAttractions);
attractionsSpinner.setAdapter(attractionsAdapter);
attractionsSpinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent,View view, int position, long id) {
// TODO Auto-generated method stub
int pos = attractionsSpinner.getSelectedItemPosition();
ImageView imageView = (ImageView) findViewById(R.id.imageViewAttraction);
String[] information = getResources().getStringArray(R.array.attractions_information_collection);
switch (position)
{
case 0:
image = R.drawable.avenue_of_stars;
imageView.setImageResource(image);
break;
case 1:
image = R.drawable.disneyland_fountain;
imageView.setImageResource(image);
break;
}
}
i get an error message saying that "image cannot be resolved as a variable"
You don't have a type declared for image. Before you start the switch declare it
int image = 0;
switch (position)
{
case 0:
image = R.drawable.avenue_of_stars;
imageView.setImageResource(R.drawable.avenue_of_stars;);
break;
case 1:
image = R.drawable.disneyland_fountain;
imageView.setImageResource(R.drawable.disneyland_fountain);
break;
}
}
Or just use the value for setImageResource()
switch (position)
{
case 0:
imageView.setImageResource(R.drawable.avenue_of_stars;);
break;
case 1:
imageView.setImageResource(R.drawable.disneyland_fountain);
break;
}
}
Or simplify it with
int image = 0;
switch (position)
{
case 0:
image = R.drawable.avenue_of_stars;
break;
case 1:
image = R.drawable.disneyland_fountain;
break;
}
imageView.setImageResource(R.drawable.disneyland_fountain);
}
The error message is telling you that you have not declared a variable called "image" and thus it doesn't know what you are trying to do when you use that word.
Change your code to be like this:
private int image = -1; // <-- you have to declare a variable to be able to use it (in java).
private imageView; // <-- declare this ImageView up here too, while your at it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.attractions_layout);
imageView = (ImageView) findViewById(R.id.imageViewAttraction); //Move findViewById() to here, calling it more than once is wasteful.
ArrayAdapter<String> attractionsAdapter = new ArrayAdapter<String> (Attractions.this, android.R.layout.simple_spinner_item, attractionEntries);
attractionsSpinner = (Spinner) findViewById (R.id.spinnerAttractions);
attractionsSpinner.setAdapter(attractionsAdapter);
attractionsSpinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent,View view, int position, long id) {
// TODO Auto-generated method stub
int pos = attractionsSpinner.getSelectedItemPosition();
String[] information = getResources().getStringArray(R.array.attractions_information_collection);
switch (position)
{
case 0:
image = R.drawable.avenue_of_stars;
imageView.setImageResource(image);
break;
case 1:
image = R.drawable.disneyland_fountain;
imageView.setImageResource(image);
break;
}
}
You must declare an int called image in order to be able to store an id into it.
Also note that calling findViewById() to get a reference to your image view every time the user selects something is wasteful, you should be getting your reference inside of onCreate() and then just using that reference for each subsequent call to setImageResource()
One last tip: It seems like you are lacking a grasp on some of the basics of Java programming and syntax. I strongly suggest you take some time now to go back and do some work familiarizing yourself with the java language a bit better before you dive into a complex Android project. Doing so will make your life much easier =).

Android setText() Different Layout File

I'm trying to use setText to show different text within the same layout everytime someone clicks a different picture.
So all the layout files stay the same the only thing that needs to change is the android:text in that layout.
I've created a class with case statement for when someone presses on a picture and then call setText().
But it looks like the setText isn't even called. because I can see my Log.v that is called within the same case statement but the text doesn't change.
PictureInfo.java
public class PictureInfo extends Activity implements OnClickListener {
private static final String TAG = "Popup";
public TextView infoText;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
infoText = (TextView)inflater.inflate(R.layout.information, null);
View a1Button = findViewById(R.id.a1);
a1Button.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.a1:
Intent a = new Intent(this, Information.class);
startActivity(a);
Log.v(TAG, "Change setText");
infoText.setText(R.string.a2_text);
break;
}
}
}
information.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/information_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/a1_text"
/>
It appears that you are inflating a layout - R.layout.information, but casting this to a TextView. I'm really not sure what you are doing there.
I think you want the destination Activity to use some text in its layout that the source Activity provides to it. Why not pass the id of the text you want displayed as an Extra on the Intent?
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.a1:
Intent a = new Intent(this, Information.class);
intent.putExtra("com.packagename.identifier", R.string.a2_text);
startActivity(a);
break;
}
}
Then in your Information Activity:
public class Information extends Activity {
...
TextView myTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
...
myTextView = (TextView) findViewById(R.id.myTextViewId);
...
Bundle extras = getIntent().getExtras();
if (extras != null) {
int textId = extras.getInt("com.packagename.identifier");
infoText.setText(textId);
}
}
}
Change the order of the code to:
//First change text
Log.v(TAG, "Change setText");
infoText.setText(R.string.a2_text);
//Then call new activity
Intent a = new Intent(this, Information.class);
startActivity(a);
And voilá!

Categories

Resources