Instantiating ImageButtons outside of Activity - android

Hi Im using several Image buttons that are displayed at the bottom of each activity, and I was wondering if there is any way to instantiate the objects outside of their respective activities Oncreate as they do the same thing in each activity but I do not want to be repeating code..
Any advice would be greatly appreciated thanks.
Marc
at the moment i am receiving a null pointer exception when trying to call the build() function.
Here is my menu where tabs will display at the bottom
package koodoo.hcp.plus;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import koodoo.hcp.utilities.Tabbuilder;
public class Hcp_Menu extends Activity implements OnClickListener {
/**
* Stores all the buttons within the HCP_Menu Activity
*/
ImageButton groupBtn;
ImageButton readingBtn;
ImageButton activityBtn;
ImageButton calendarBtn;
ImageButton ongoingBtn;
Context context = this;
View view = new View(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hcp__menu);
Tabbuilder tb = new Tabbuilder();
tb.build(view, context);
/**
* Create Activity Buttons
*/
//group button
groupBtn = (ImageButton) findViewById(R.id.imageButtonGroup);
groupBtn.setOnClickListener(this);
//reading button
readingBtn = (ImageButton) findViewById(R.id.imageButtonReading);
readingBtn.setOnClickListener(this);
//activity button
activityBtn = (ImageButton) findViewById(R.id.imageButtonActivity);
activityBtn.setOnClickListener(this);
//calendar button
calendarBtn = (ImageButton) findViewById(R.id.imageButtonCalender);
calendarBtn.setOnClickListener(this);
//ongoing button
ongoingBtn = (ImageButton) findViewById(R.id.imageButtonOngoing);
ongoingBtn.setOnClickListener(this);
/**
* create Tab buttons
*//*
//dash tab
dashTab = (ImageButton) findViewById(R.id.dashButton);
dashTab.setOnClickListener(this);
//stats tab
statsTab = (ImageButton) findViewById(R.id.statsButton);
statsTab.setOnClickListener(this);
//invite tab
inviteTab = (ImageButton) findViewById(R.id.inviteButton);
inviteTab.setOnClickListener(this);
//settings tab
settingsTab = (ImageButton) findViewById(R.id.settingsButton);
settingsTab.setOnClickListener(this);
//log tab
logTab = (ImageButton) findViewById(R.id.logButton);
logTab.setOnClickListener(this);*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hcp__menu, menu);
return true;
}
/**
* onclick function for each button on the Activity.
* #param v
*/
#Override
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.imageButtonGroup: i = new Intent(this, Group.class);
startActivity(i);
break;
case R.id.imageButtonActivity: i = new Intent(this, Activities.class);
startActivity(i);
break;
case R.id.imageButtonCalender: i = new Intent(this, Calender.class);
startActivity(i);
break;
case R.id.imageButtonOngoing: i = new Intent(this, Ongoing.class);
startActivity(i);
break;
case R.id.imageButtonReading: i = new Intent(this, Reading.class);
startActivity(i);
break;
default:
break;
}
}
}
Tabbuilder class
package koodoo.hcp.utilities;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.ImageButton;
import koodoo.hcp.plus.Hcp_Menu;
import koodoo.hcp.plus.Invite;
import koodoo.hcp.plus.Log;
import koodoo.hcp.plus.R;
import koodoo.hcp.plus.Settings;
import koodoo.hcp.plus.Stats;
/**
* Created by Marc Davies on 18/09/2013.
*/
public class Tabbuilder {
ImageButton dashTab;
ImageButton statsTab;
ImageButton inviteTab;
ImageButton settingsTab;
ImageButton logTab;
public void build(View v, final Context context) {
/**
* create Tab buttons
*/
//dash tab
dashTab = (ImageButton) v.findViewById(R.id.dashButton);
dashTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, Hcp_Menu.class);
}
});
//stats tab
statsTab = (ImageButton) v.findViewById(R.id.statsButton);
statsTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, Stats.class);
}
});
//invite tab
inviteTab = (ImageButton) v.findViewById(R.id.inviteButton);
inviteTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, Invite.class);
}
});
//settings tab
settingsTab = (ImageButton) v.findViewById(R.id.settingsButton);
settingsTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, Settings.class);
}
});
//log tab
logTab = (ImageButton) v.findViewById(R.id.logButton);
logTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, Log.class);
}
});
}
}

Create a base activity and instantiate buttons in it. Now let other activities inherit the base activity. Dont forget to handle the layouts properly though

you can make a utils class with static initiate Methods.
Those would take a context and an image and return an imageButton. You would still call that in the onCreate of the corresponding Activity, but you wouldnt have to copy paste your code anymore.

Related

Move to a particular tab of bottom navigation from an activity

I am working on a project in which I used Bottom Navigation Bar. On Nav Bar, I used 4 menu items(Profile, Home, Reservation, Logout), for them I used fragment.
Moving to Home, a list of restaurant will be appear. When the user tap on any restaurant from the list, an activity( name ReservationInfo ) will be opened. There are 3 edit text fields and a button in ReservationInfo activity. When the user click on the button it will move to the fragment(Reservation) which is placed on third position of bottom nav bar.
The question is how to move from activity to fragmentand the fragment is set to the 3rd menu item of the bottom navigation bar.
Here is the code:
Resrvation.java
package restaurantlocator.application;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class Resrvation extends AppCompatActivity {
Button reserve;
ImageButton imageButton;
EditText food, chooseTime, date;
DataBaseHelper db;
// ReservationInfo reservationInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resrvation);
db = new DataBaseHelper(this);
food = (EditText) findViewById(R.id.etfood);
chooseTime = (EditText) findViewById(R.id.ettime);
date = (EditText) findViewById(R.id.edate);
reserve = (Button) findViewById(R.id.btnreserve);
imageButton = (ImageButton) findViewById(R.id.imgButton);
imageButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
}
});
//reservationInfo = new ReservationInfo();
reserve.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!emptyValidation()) {
db.AddData(new Data(food.getText().toString(),
chooseTime.getText().toString(),
date.getText().toString()));
AlertMessage();
food.setText(null);
chooseTime.setText(null);
date.setText(null);
} else {
Toast.makeText(Resrvation.this, "Empty Fields", Toast.LENGTH_SHORT).show();
}
}
private void AlertMessage() {
AlertDialog.Builder builder = new AlertDialog.Builder(Resrvation.this);
builder.setTitle("Reserved!");
builder.setMessage("A notification will be send on your device regarding your reservation.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//move to fragment ReservationInfo
}
});
builder.show();
}
});
}
private boolean emptyValidation() {
if (TextUtils.isEmpty(food.getText().toString()) || TextUtils.isEmpty(chooseTime.getText().toString())) {
return true;
} else {
return false;
}
}
}
BottomNavigation.java
package restaurantlocator.application;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;
public class BottomNavigation extends AppCompatActivity {
private BottomNavigationView mMianNav;
private FrameLayout mMainFrame;
private HomeActivity homeActivity;
private ReservationInfo reservationInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_navigation);
startService(new Intent(this, NotificationService.class));
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMianNav = (BottomNavigationView) findViewById(R.id.main_nav);
homeActivity = new HomeActivity();
reservationInfo = new ReservationInfo();
setFragment(homeActivity);
//Listener for handling selection events on bottom navigation items
mMianNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.tohome:
setFragment(homeActivity);
return true;
case R.id.toresrvation:
setFragment(reservationInfo);
return true;
case R.id.tologout:
logout();
return true;
default:
return false;
}
}
private void logout() {
Intent intent = new Intent(BottomNavigation.this, Registration.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
});
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
fragmentTransaction.addToBackStack(null);
}
public void onBackPressed() {
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
finish();
}
}
Solution:
Follow the steps below:
Step1: Add the lines as shown in below reserve click listener
reserve.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
....
....
Intent intent = new Intent(Resrvation.this, BottomNavigation.class);
intent.putExtra("FromReservation", "1");
startActivity(intent);
....
}
}
Next, Step2: In your write the below code in onCreate() of BottomNavigation class:
Intent i = getIntent();
String data = i.getStringExtra("FromReservation");
if (data != null && data.contentEquals("1")) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, new ReservationInfo());
fragmentTransaction.commitNow();
mMianNav.setSelectedItemId(R.id.toresrvation);
}
Try it, If any problem, do comment below.
All you need to do is just use startActivityForResults instead of startActivity when user selects a restaurant from your home fragment.
e.g :
startActivityForResult(yourIntent, 1000);
And on the reservationInfo Activity in the onClick function use
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
And then in your first activity override onActivityResult() method and set the selected fragment as reservation if request code matches
e.g:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1000) {
mMianNav.setSelectedItemId(R.id.toresrvation);
}
}

Android:Not able to write on textview of main activity when comming from different activity

I created a simple demo program in which there is start button.When i click on start button on main (Home) screen "hi all" appended to Text view.It works fine, But when I change the activity from selecting action bar menu and if again come on the home screen by selecting the action bar home menu then it will not show the "hi all " Message when I click on the Start button.
package com.example.testdemo;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView logArea;
private TextView log;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
ActionBar actionBar = getActionBar();
log = new TextView(MainActivity.this);
log.append("Heollosdfsjdf" + "\n");
#SuppressWarnings("deprecation")
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
params.gravity = Gravity.LEFT;
log.setLayoutParams(params);
log.setGravity(Gravity.CENTER);
LinearLayout chat = (LinearLayout) findViewById(R.id.main_linear_view);
log.setMovementMethod(new ScrollingMovementMethod());
chat.addView(log);
setDefault();
}
public void setDefault(){
Button btn = (Button) findViewById(R.id.start_recording_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startWriting(v);
}
});
}
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;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
goHome();
return true;
case R.id.general_setting:
generalSetting();
return true;
case R.id.server_settings:
serverSetting();
return true;
case R.id.audio_settings:
audioSetting();
return true;
default:
break;
}
return true;
}
private void goHome() {
Intent i = new Intent(MainActivity.this, home.class);
startActivity(i);
finish();
}
private void generalSetting() {
Intent i = new Intent(MainActivity.this, general.class);
startActivity(i);
finish();
}
private void audioSetting() {
Intent i = new Intent(MainActivity.this, audio.class);
startActivity(i);
finish();
}
private void serverSetting() {
Intent i = new Intent(MainActivity.this, server.class);
startActivity(i);
finish();
}
public void startWriting(View view) {
logMessage("Hi all");
}
private void logMessage(String msg) {
log.append(msg + "\n");
final int scrollAmount = log.getLayout().getLineTop(log.getLineCount())- log.getHeight();
if (scrollAmount > 0)
log.scrollTo(0, scrollAmount);
else
log.scrollTo(0, 0);
}
}
it's because you called 'finish' when you changed activities. calling the main activity again reloads its view to its initial state.
In addition to MetaSnarf's answer: For a deeper understanding of the "Android Task Stack" and saving of the current Activity state, you might want to take a look at http://developer.android.com/guide/components/tasks-and-back-stack.html

I can't make a button appear (possibly issue with static referencing)

I want to make a button appear in the MenuActivity layout but the deciding if statement is in the CapitalReceiver class. I've tried adding 'static' to various variables but it didn't work. Please help!
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.text.format.DateFormat;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class MenuActivity extends Activity {
String status;
Boolean verified = false;
String textColour = "#000000";
TextView mTvCapital;
ArrayAdapter<String> mAdapter;
Intent mServiceIntent;
CapitalReceiver mReceiver;
IntentFilter mFilter;
String country = "7ec47294ff3d8b74";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_layout);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Button IDButton = (Button) findViewById(R.id.getIt);
Button RefreshButton = (Button) findViewById(R.id.refresh);
long updateTimeMillis = System.currentTimeMillis();
String updateTime = (String) DateFormat.format("hh:mm", updateTimeMillis);
//If application has been submitted//
if(preferences.contains("first_middle_store") & !(verified)) {
status = "Status: Application pending. Last updated: " + updateTime;
IDButton.setVisibility(View.GONE);
RefreshButton.setVisibility(View.VISIBLE);
textColour = "#000000";
}
//If application has not been submitted
else {
status = "Status: Application not yet submitted";
IDButton.setVisibility(View.GONE);
RefreshButton.setVisibility(View.GONE);
textColour = "#000000";
}
TextView text=(TextView)findViewById(R.id.application_status);
text.setTextColor(Color.parseColor(textColour));
text.setText(status);
Button btnNextScreen = (Button) findViewById(R.id.verify);
//Listening to verify event
btnNextScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent nextScreen = new Intent(getApplicationContext(), VerifyActivity.class);
startActivity(nextScreen);
}
});
Button btnNextScreen2 = (Button) findViewById(R.id.how);
//Listening to HowItWorks event
btnNextScreen2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent nextScreen2 = new Intent(getApplicationContext(), HowItWorksActivity.class);
startActivity(nextScreen2);
}
});
//Listening to IDbutton event
IDButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent nextScreen3 = new Intent(getApplicationContext(), IDActivity.class);
startActivity(nextScreen3);
}
});
}
#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, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu_layout, container, false);
return rootView;
}
}
public void refresh(View view) {
// Getting reference to TextView
mTvCapital = (TextView) findViewById(R.id.tv_capital);
mTvCapital.setText("hello");
// Creating an intent service
mServiceIntent = new Intent(getApplicationContext(), CapitalService.class);
mServiceIntent.putExtra(Constants.EXTRA_ANDROID_ID, country);
// Starting the CapitalService to fetch the capital of the country
startService(mServiceIntent);
// Instantiating BroadcastReceiver
mReceiver = new CapitalReceiver();
// Creating an IntentFilter with action
mFilter = new IntentFilter(Constants.BROADCAST_ACTION);
// Registering BroadcastReceiver with this activity for the intent filter
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(mReceiver, mFilter);
}
// Defining a BroadcastReceiver
private static class CapitalReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String capital = intent.getStringExtra(Constants.EXTRA_APPROVAL);
if(capital == "YES") {
//status = "Status: Application Approved";//
//IDButton.setVisibility(View.VISIBLE);//
}
else if(capital == "NO"){
//status = "Status: Application Denied";//
}
}
}
}
Just glancing over your code, a possible solution (and perhaps not the best) would be to make the ID Button variable global. You would then instantiate it in the onCreate whilst still allowing it to be manipulated in other classes in this MenuActivity.
I hope this helps.

Only one of three buttons working

Hey I am trying to get different buttons to open up different pages in a android project but only on of the buttons is opening up a new page.
I am new to programming so my terminology may not be correct but I was following a youtube tutorial and it showed how to create a button and make it open up a new page. I tried to do this for multiple buttons but I think I am making the mistake in the main activity. Sorry if I haven't provided the write information to help me solve the problem.
package test.activity.today;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityTutorialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.next_button);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), NextActivity.class);
v.getContext().startActivity(myIntent);
}
});
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.question_button);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Question.class);
v.getContext().startActivity(myIntent);
}
});
}
public void onCreate2(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.owner_cost);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Owner.class);
v.getContext().startActivity(myIntent);
}
});
}
}
you should only have one onCreate() method.. check android activity's life cycle to understand it
package test.activity.today;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityTutorialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.next_button);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), NextActivity.class);
v.getContext().startActivity(myIntent);
}
});
Button question = (Button) findViewById(R.id.question_button);
question.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Question.class);
v.getContext().startActivity(myIntent);
}
});
Button ownerCost = (Button) findViewById(R.id.owner_cost);
ownerCost.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Owner.class);
v.getContext().startActivity(myIntent);
}
});
}
}
You are duplicating the onCreate() method... this method is called natively by Android and, thus, none of your other methods will get called (you'd have other problems if they were). To create more than one button, you need to add new buttons to your layout and then add them to onCreate().
There is another way to implement the onClick.
In your layout you can specify the function to call onClick
<ImageButton
android:id="#+id/imageButtonNext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#color/back_color"
android:onClick="RegistraterCompanyOnClick"
android:src="#drawable/ic_next" />
Then in your activity you can filter which button by it's ID. See below.
public void RegistraterCompanyOnClick(View v){
switch(v.getId()){
case R.id.imageButtonNext1:
String cname = company.getText().toString();
if (cname.length()== 0){
message = getString(R.string.company_required);
ShowDialog(message);
}
else{
company_name = company.getText().toString();
VerifyClient(company_name);
}
break;
case R.id.imageButtonInfo1:
//message = getString(R.string.registration_info);
message = "Device ID:\n" + deviceID;
// TODO Auto-generated method stub
ShowDialog(message);
break;
case R.id.imageButtonHelp1:
message = getString(R.string.registration_contact);
// TODO Auto-generated method stub
ShowDialog(message);
break;
case R.id.imageButtonPrevious1:
Intent resultIntent = new Intent();
// TODO Auto-generated method stub
resultIntent.putExtra("company_name", company.getText().toString());
resultIntent.putExtra("company_id", companyID);
resultIntent.putExtra("location_name", location_name);
resultIntent.putExtra("location_id", locationID);
setResult(Activity.RESULT_CANCELED, resultIntent);
finish();
default:
break;
}
}

Relative Layouts and Linear Layouts

how do i embed a tab layout within a button. My main layout is a linear layout, but i don't know how to program the main activity.java class. Could anybody help me get started this is what my main.java code looks like right now
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
public class Remote_DocActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//private static final String TAG = "Remote_Doc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View Patient_Button = findViewById(R.id.patientButton);
Patient_Button.setOnClickListener(this);
View Doctor_Button = findViewById(R.id.doctorButton);
Doctor_Button.setOnClickListener(this);
View About_Option = findViewById(R.id.aboutButton);
About_Option.setOnClickListener(this);
View Exit_Option = findViewById(R.id.exit);
Exit_Option.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.aboutButton:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}
you can create tabs with the help of TabActivity class. You can take help from android-wireless-application-development book's unit 3 chapter 8 example.

Categories

Resources