Reaching button in title bar in subactivities - android

When creating a titlebar with a button, which is common in all activities e.g. title bar created in tabactivities. how is it possible to reach the button in all of the sub activities??
public class tabActivity extends TabActivity implements OnClickListener{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
c = this;
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.tabactivity);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Settings",
res.getDrawable(R.drawable.preferences)).setContent(
new Intent(this, Settings.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("About",
res.getDrawable(R.drawable.newspaper)).setContent(
new Intent(this, About.class)));
This is here where i initialize my tabs, and the custom title with buttons..
And in this class i would like to reach the buttons in the custom title.:
public class About extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
ImageView imag = (ImageView) findViewById(R.id.Position);
imag.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("heeey");
}
});
}
The listener doesnt work??
Hooow is this possible??

public class tabActivity extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
c = this;
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.tabactivity);
ImageView imag = (ImageView) findViewById(R.id.Position);
imag.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tabActivity.listener.onClick(v);
}
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Settings",
res.getDrawable(R.drawable.preferences)).setContent(
new Intent(this, Settings.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("About",
res.getDrawable(R.drawable.newspaper)).setContent(
new Intent(this, About.class)));
}
public static void setListner(OnClickListener listener)
{
tabActivity.listner = listener;
}
main activity does not implements eventListener
public class About extends Activity implements OnClickListener
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
});
public void onResume()
{
tabActivity.setListener(this);
}
}
code goes like this. It's hard to explain

What is the purpose of that? The event controll is available in main activity.

Related

No Button Feedback In Android Studio

no button feedback in android studio, i just want to guide from one activity to another! :(
no errors
here's the code
public abstract class OrderActivity extends AppCompatActivity {
private Button clickButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
clickButton =(Button) findViewById(R.id.IDbutton2);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity();
}
});
}
public void openActivity(){
Intent intent = new Intent(OrderActivity.this, orderPage.class);
startActivity(intent);
}
}
using nexus 6 emulator
please help guys
You haven't set a content view, so your OrderActivity has no layout. As such, there is no view with ID IDbutton2. This causes clickButton to be null, and not do anything.
You can see a very similar example in the Android training (I've removed the irrelevant bits):
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
}
}

Button visibility according to actvities

I have three activities A.B and C. I have a button in activity C. My requirement is that the button should only visible when going from activity B to C. The button should be invisble when going from A to C. Please help me.
Activity A
public class A extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
// TODO: 5/5/2018 consider findViewById
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(A.this, C.class);
intent.putExtra(KEY_EXTRA, FROM_A);
startActivity(intent);
}
});
}
}
Activity B
public class B extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
// TODO: 5/5/2018 consider findViewById
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(B.this, C.class);
intent.putExtra(KEY_EXTRA, FROM_B);
startActivity(intent);
}
});
}
}
Activity C
public class C extends AppCompatActivity {
Button button;
public static int FROM_A = 1;
public static int FROM_B = 2;
public static String KEY_EXTRA = "KEY_EXTRA";
int activityStartedFrom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
// TODO: 5/5/2018 consider findViewById
activityStartedFrom = getIntent().getIntExtra(KEY_EXTRA, FROM_B);
button.setVisibility(activityStartedFrom == FROM_B ? View.VISIBLE : View.GONE);
}
}

extends another activity class and reuse listener codes to views such as buttons etc

I have created an activity called ButtonActivity that has a lot of buttons and listeners. I want to create another activity TwoButtonsActivity to extend ButtonActivity so that the listeners I created can be resused.
TwoButtonsActivity is similar to ButtonActivity but with small changes.
Is this possible?
When I execute the code, I find that the extended activity do not respond to button click.
Here is the base activity:
public class ButtonActivity extends Activity {
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
button.setText("Got Pressed:" + ++count);
}
});
}
}
Below is the extends Activity:
public class TwoButtonsActivity extends ButtonActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
you can have a activity with listeners same below
public class ButtonActivity extends Activity {
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
}
public void mylistener1(View v)
{
switch(v.getid()){
case R.id.button:{
//do somthings
}break;
}}
public class TwoButtonsActivity extends ButtonActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
and in layout of TwoButtonsActivity(main) you set in tag of your button android:onclick="mylistener1"
and you should set to any button or view that you want it use this listener

custom titlebar button in tabactivity android

Heey. How to add button in custom title bar through TabActivity ? I can set title TextView depending on which subactivity is called. But i cant figure out how to add button ?
My TabActivity:
public class TabsManager extends TabActivity {
static TabsManager mTab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTab = this;
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.tabhostbottom);
}
public void setMyTitle(String string) {
((Activity) mTab).getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
TextView textView = (TextView) findViewById(R.id.header);
textView.setText(string);
}
And my subactivity:
#Override
protected void onResume() {
super.onResume();
TabsManager.mTab.setMyTitle("Activity1");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_club);
TabsManager.mTab.setMyTitle("Activity1");
}

navigation from one screen to other screen in android

public class Check extends Activity implements OnClickListener {
//private check2 check2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button usemirror = (Button)findViewById(R.id.widget28);
usemirror.setOnClickListener(this);
}
public void onClick(View view){
Intent mi = new Intent(this , check2.class);
startActivity(mi);
}
}
package com.exaple;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class check2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
Toast.makeText(
this,
"Welcome to second page", Toast.LENGTH_LONG).show();
finish();
}
}
ANDROID manifest.xml
<activity android:name=".check2" >
I am doing this but doesn't show me the message written in my other activities
#Uttam Didn't you got super not called exception?
I suppose you must call super.onCreate() in your check2.Not calling this in your check2 is causing Force Close
public class check2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //first call super.onCreate()
Toast.makeText(
this,
"Welcome to second page", Toast.LENGTH_LONG).show();
finish();
}
I suppose you still need to add:
#Override
public void onPause(){
super.onPause();
}
to both first and second activities.
Please change your first activity code as below
public class Check extends Activity implements OnClickListener {
//private check2 check2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button usemirror = (Button)findViewById(R.id.widget28);
usemirror.setOnClickListener(this);
}
public void onClick(View view){
Intent mi = new Intent(Check.this , check2.class);
startActivity(mi);
}
}
& second activity code as below
public class check2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //first call super.onCreate()
Toast.makeText(
this,
"Welcome to second page", Toast.LENGTH_LONG).show();
finish();
}

Categories

Resources