custom titlebar button in tabactivity android - 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");
}

Related

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);
}
}

Find Id of View in Child Activity

I have an issue , need your help , my question is : I have a multiple activity , so i have created one abstract class which hold the view . Now my question i am able to pass the view to the Base Activity but how to finds view id in Child Activity Class.
I Tried like this :
public abstract class BaseActivity extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
}
public abstract int getLayoutResourceId();
}
My Activity Class:
public class ServiceActivity extends BaseActivity {
Button startSerice_btn, stopService_btn;
MyService myService;
#Override
public int getLayoutResourceId() {
return R.layout.service_xml;
}
}
You can access your views after the super.onCreate() call in the ServiceActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Access them here
}
I think you no need to set content view in BaseActivity like below :
public abstract class MyAppBaseActivity extends AppCompatActivity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
And you just extends Activity like below :
// for MyCustomActivity1
public class MyCustomActivity1 extends MyAppBaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onClick(View view) {
}
}
// for MyCustomActivity2
public class MyCustomActivity2 extends MyAppBaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onClick(View view) {
}
}

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

Inheritance issues with Android annotations and OnClickListener

This is the inheritance that I'm using
GenericActivity -> GraphGenericActivity -> NormalActivity
I have an options menu with contains a help button which show a help view over the current one and this works fine however it's the close button that doesn't work, with #Click I doesn't work on any of the views and if I register a onClickListener the old fashionned way It only workds on Activities that extend directly from "GenericActivity
GENERIC ACTIVITY
#EActivity
#OptionsMenu(R.menu.menu_generic)
public abstract class GenericActivity extends Activity{
public static final String TAG = "GenericActivity";
protected Context context;
protected LayoutInflater vi;
protected View helpView;
#ViewById
protected RelativeLayout rootLayout;
#ViewById
protected Button closeHelpButton;
protected abstract int getHelpLayoutInt();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#AfterViews
protected void afterViews() {
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
helpView = vi.inflate(this.getHelpLayoutInt(), null);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
helpView.setVisibility(View.GONE);
if (helpView != null) {
rootLayout.addView(helpView, layoutParams);
}
final Button button = (Button) findViewById(R.id.closeHelpButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
#Trace
public void onClick(View v) {
Toast.makeText(context, "close help", Toast.LENGTH_SHORT).show();
if (helpView != null) {
helpView.setVisibility(View.GONE);
}
}
});
}
#OptionsItem
protected boolean menuHelp() {
if (helpView != null) {
if (helpView.getVisibility() == View.GONE) {
helpView.setVisibility(View.VISIBLE);
} else {
helpView.setVisibility(View.GONE);
}
}
return true;
}
}
CHILD ACTIVITY
#EActivity(R.layout.activity_start_screen)
public class StartScreen extends GenericActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(false);
CALog.i("onCreateFinished");
}
#Trace
#Override
protected void onDestroy() {
domboxTouchServiceManager.unbindFromDomboxService();
super.onDestroy();
}
#Override
protected int getHelpLayoutInt() {
return R.layout.layout_start_screen_help;
}
}

Reaching button in title bar in subactivities

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.

Categories

Resources