How to show Actionbar menu item on top in PreferenceActivity? - android

Help me out to show Actionbar menu item on the Top of the Actionbar. Without showing on overflow drop down menulist.kindly give me a suggestion to do like that?

Use this in your activity
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
LayoutParams lp = new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
View customNav = LayoutInflater.from(this).inflate(R.layout.img, null);
EditText et = (EditText) customNav.findViewById(R.id.et);
temppaths = new ArrayList<String>();
Button iv = (Button) customNav.findViewById(R.id.iv);
iv.setClickable(true);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);
and xmlLayout name it img.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:gravity="right" >
<EditText android:id="#+id/et"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:gravity="center"
android:layout_gravity="right"
android:hint="info#xxxxxx, 1800xxxxx"
android:background="#FF8000" />
<Button
android:id="#+id/iv"
android:layout_gravity="right"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:text="Search"
android:background="#FF8000" />
</LinearLayout >

okk then
add these methods in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 0, 0, "LogOut").setShortcut('0', 'o')
.setIcon(android.R.drawable.ic_menu_edit);
menu.add(0, 1, 0, "Save").setShortcut('1', 's')
.setIcon(android.R.drawable.ic_menu_save);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
Toast.makeText(getApplicationContext(), "Log out clicked", 0).show();
// save();
// showNewOrOpenDialog();
break;
case 1:
Toast.makeText(getApplicationContext(), "Save", 0).show();
// save();
break;
}
return false;
}

Related

Pop up window from menu overflow item selected android

I am trying to display a pop up window when i click on a menu overflow item. I have written some code also for that. But it outputs nothing.
Here is some code:
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.settings_id:
displayPopupWindow();
return true;
case R.id.about_us_id:
return true;
case R.id.logout_id:
startActivity(new Intent(HomePage.this,MainActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void displayPopupWindow() {
PopupWindow popup = new PopupWindow(this);
View layout = getLayoutInflater().inflate(R.layout.popup, null);
popup.setContentView(layout);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
My Xml file Code is below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#color/colorPrimary">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="165dp"
android:id="#+id/textView"
tools:text="This is a Pop Up Window!"
android:textSize="36sp"
android:textStyle="normal|bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:textAlignment="center"
android:fontFamily="sans-serif" />
</RelativeLayout>
Where is the error? Please help me.
Here is your solution,
MainActivity.java
public class MainActivity extends Activity {
private PopupWindow mPopupWindow;
private Button btnClosePopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
break;
case R.id.computer:
Toast.makeText(getBaseContext(), "You selected Computer", Toast.LENGTH_SHORT).show();
break;
case R.id.gamepad:
Toast.makeText(getBaseContext(), "You selected Gamepad", Toast.LENGTH_SHORT).show();
break;
case R.id.camera:
Toast.makeText(getBaseContext(), "You selected Camera", Toast.LENGTH_SHORT).show();
initiatePopupWindow();
break;
case R.id.video:
Toast.makeText(getBaseContext(), "You selected Video", Toast.LENGTH_SHORT).show();
break;
case R.id.email:
Toast.makeText(getBaseContext(), "You selected EMail", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private void initiatePopupWindow() {
try {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,
(ViewGroup) findViewById(R.id.popup_element));
mPopupWindow = new PopupWindow(layout, 300, 370, true);
mPopupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mPopupWindow.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
popup.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp">
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="Hello!" />
<Button
android:id="#+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>

Android - Trying to create ImageView Array and add drawable resource to it in code

I am trying to create an array of ImageView controls and add drawable resources to it. I am getting
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.ImageView.setBackgroundResource(int)' on a
null object reference
Here is the code:
public class MultipleChoice extends ActionBarActivity {
TextView Left;
TextView Operator;
TextView Right;
TextView GameTimer;
TextView CorrectAnswers;
ImageView imageView[] = new ImageView[11];
int imageViewNumber;
Button btnStart;
int intLeft;
int intOperator;
int intRight;
int intCorrectAnswer;
int intCorrectAnswers;
String strCorrectAnswer;
CountDownTimer problemtimer = new CountDownTimer(10000,1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
NextProblem();
}
};
CountDownTimer gametimer = new CountDownTimer(60000,1000) {
#Override
public void onTick(long millisUntilFinished) {
GameTimer.setText("Time Left: " + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
problemtimer.cancel();
gametimer.cancel();
GameTimer.setText("Time Left: 0");
btnStart.setEnabled(true);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple_choice);
SetStartupValues();
}
private void SetStartupValues(){
Left = (TextView) findViewById(R.id.Left);
Operator = (TextView) findViewById(R.id.Operator);
Right = (TextView) findViewById(R.id.Right);
GameTimer = (TextView) findViewById(R.id.Timeleft);
CorrectAnswers = (TextView) findViewById(R.id.CorrectAnswers);
btnStart = (Button) findViewById(R.id.btnStart);
Left.setText("");
Operator.setText("");
Right.setText("");
GameTimer.setText("Time Left: 60");
CorrectAnswers.setText("Correct: 0");
intCorrectAnswers = 0;
}
#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);
}
public void btnStart_Click(View view) {
// Do something in response to button click
SetStartupValues();
NextProblem();
gametimer.start();
btnStart.setEnabled(false);
}
public void NextProblem(){
intLeft = new Random().nextInt(100);
intRight= new Random().nextInt(100);
intOperator= new Random().nextInt(3);
Left.setText(String.valueOf(intLeft));
Right.setText(String.valueOf(intRight));
imageViewNumber=0;
switch(intOperator){
case 0:
Operator.setText("+");
intCorrectAnswer = intLeft + intRight;
break;
case 1:
Operator.setText("-");
intCorrectAnswer=intLeft-intRight;
break;
case 2:
Operator.setText("X");
intCorrectAnswer=intLeft*intRight;
break;
}
strCorrectAnswer = String.valueOf(intCorrectAnswer);
GetNumbers(strCorrectAnswer);
problemtimer.start();
}
private void GetNumbers(String strNumber){
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
int L = strNumber.length();
String c;
for (int i = 0;i<L-1;i++){
c=strNumber.substring(i,i+1);
switch (c){
case "1":
imageView[imageViewNumber].setBackgroundResource(R.drawable.one);
ll.addView(imageView[imageViewNumber]);
break;
case "2":
imageView[imageViewNumber].setBackgroundResource(R.drawable.two);
ll.addView(imageView[imageViewNumber]);
break;
case "3":
imageView[imageViewNumber].setBackgroundResource(R.drawable.three);
ll.addView(imageView[imageViewNumber]);
break;
case "4":
imageView[imageViewNumber].setBackgroundResource(R.drawable.four);
ll.addView(imageView[imageViewNumber]);
break;
case "5":
imageView[imageViewNumber].setBackgroundResource(R.drawable.five);
ll.addView(imageView[imageViewNumber]);
break;
case "6":
imageView[imageViewNumber].setBackgroundResource(R.drawable.six);
ll.addView(imageView[imageViewNumber]);
break;
case "7":
imageView[imageViewNumber].setBackgroundResource(R.drawable.seven);
ll.addView(imageView[imageViewNumber]);
break;
case "8":
imageView[imageViewNumber].setBackgroundResource(R.drawable.eight);
ll.addView(imageView[imageViewNumber]);
break;
case "9":
imageView[imageViewNumber].setBackgroundResource(R.drawable.nine);
ll.addView(imageView[imageViewNumber]);
break;
case "0":
imageView[imageViewNumber].setBackgroundResource(R.drawable.zero);
ll.addView(imageView[imageViewNumber]);
break;
}
}
}
The error occurs in the Switch statement on the lines like
imageView[imageViewNumber].setBackgroundResource(R.drawable.zero);
What am I doing wrong?
Here is my Layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MultipleChoice"
android:minWidth="30dp"
android:focusableInTouchMode="false">
<TextView android:text="#string/LeftText" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Left"
android:layout_below="#+id/Timeleft"
android:layout_toLeftOf="#+id/Operator"
android:layout_toStartOf="#+id/Operator" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/OperatorText"
android:id="#+id/Operator"
android:layout_below="#+id/Timeleft"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/RightText"
android:id="#+id/Right"
android:layout_below="#+id/Timeleft"
android:layout_toRightOf="#+id/Operator"
android:layout_toEndOf="#+id/Operator" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/StartButton"
android:id="#+id/btnStart"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="btnStart_Click"
android:enabled="true"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/TimeLeft"
android:id="#+id/Timeleft"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Correct: 0"
android:id="#+id/CorrectAnswers"
android:layout_alignTop="#+id/Timeleft"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/LinearContainer"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:baselineAligned="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/linearLayout1"
android:layout_weight="1"
android:gravity="center_horizontal">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/linearLayout2"
android:gravity="center_horizontal">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/linearLayout3"
android:gravity="center_horizontal">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
You dont have objects in your "imageView" array.
Initialize it with values:
for(int i = 0; i < imageView.lenght; i++) imageView[i] = new ImageView(context);
So you can use then properly.

Toolbar Item Click

I am using toolbar in my app. Toolbar layout looks like this. I add imageButton inside toolbar as menu item. (Is that is a problem?) As I am new to Toolbar I don't know if this is the right way.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_gravity="top"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:minHeight="?attr/actionBarSize"
android:gravity="right"
android:background="?attr/colorPrimaryDark">
<ImageButton
android:src="#drawable/ic_action_social_share"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="right"
android:layout_marginRight="8dp"
android:background="?selectableItemBackground"
android:id="#+id/shareButton" />
<ImageButton
android:src="#drawable/ic_tag"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="right"
android:layout_marginRight="8dp"
android:background="?selectableItemBackground"
android:id="#+id/tagButton" />
<ImageButton
android:src="#drawable/ic_action_action_bookmark"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="right"
android:layout_marginRight="8dp"
android:background="?selectableItemBackground"
android:id="#+id/bookmarkButton" />
</android.support.v7.widget.Toolbar>
And in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsdetail_activity);
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mActionBarToolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
And onOptionItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
case R.id.shareButton:
Log.d(TAG, "SHARE BUTTON");
break;
}
return true;
}
The click on shareButton in toolbar is not firing. Can anyone tell the reason for this?
I ended up by using the following code
ImageButton imageButton = (ImageButton) toolbar.findViewById(R.id.shareButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I don't know if this the correct way.But it works
change
case R.id.shareButton:
Log.d(TAG, "SHARE BUTTON");
break;
to
case android.R.id.shareButton:
Log.d(TAG, "SHARE BUTTON");
break;
Simply use this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
actionbar = getActionBar();
actionbar.setDisplayShowCustomEnabled(true);
actionbar.setCustomView(R.layout.action_bar_layout);
btnClick = (Button) findViewById(R.id.btnClick);
appIcon = (ImageView) findViewById(R.id.appIcon);
appName = (TextView) findViewById(R.id.appName);
appName.setOnClickListener(this);
appIcon.setOnClickListener(this);
btnClick.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.appName:
Toast.makeText(LauncherActivity.this, "You Clicked on App Name", Toast.LENGTH_LONG).show();
break;
case R.id.appIcon:
Toast.makeText(LauncherActivity.this, "You Clicked on App Icon", Toast.LENGTH_LONG).show();
break;
case R.id.btnClick:
Toast.makeText(LauncherActivity.this, "You Clicked on Button", Toast.LENGTH_LONG).show();
break;
}
}
See my git project here

Android ClickListeners not firing from a PopupWindow

I've got a popupWindow that is triggered from the Activity Bar in Main Event. The buttons in the popup window are not triggering their respective listeners in showPopup(). Much of this popupWindow structure works fine when initiated from a fragment. I cannot identify the root cause of this. Any suggestions? Thanks.
public class MainActivity extends Activity{
private final static String TAB_KEY_INDEX = "TAB_KEY";
public static Context appContext;
private PopupWindow popupWindow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appContext = getApplicationContext();
//put Actionbar in tab mode
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//set titles for tabs
ActionBar.Tab tab1 = actionBar.newTab().setText("Tab1");
//create instances of each of the fragments
Fragment tab1Fragment = new Tab1Fragment();
//attach those fragment instances to their respective tabs
tab1.setTabListener(new MyTabsListener(tab1Fragment));
//add each tab to the ActionBar
actionBar.addTab(tab1);
if (savedInstanceState == null){//...do nothing
}else if (savedInstanceState != null){
actionBar.setSelectedNavigationItem(savedInstanceState.getInt(TAB_KEY_INDEX,0));
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menuitem_popup:
showPopup();
return true;
}return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt(TAB_KEY_INDEX, getActionBar().getSelectedNavigationIndex());
}
private void showPopup() {
Button btnDismiss, btnSaveRecord;
try {
LayoutInflater layoutInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, null);
popupWindow = new PopupWindow(layout, 580, 500, true);
popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 40);
btnDismiss=(Button)MainActivity.this.findViewById(R.id.btnDismissxml);
btnDismiss.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View arg0) {
popupWindow.dismiss();
}
});
btnSaveRecord=(Button)MainActivity.this.findViewById(R.id.btnSaveRecordxml);
btnSaveRecord.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
saveRecord();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public void saveRecord(){
Toast.makeText(MainActivity.this.getApplicationContext(), "Event saveRecord() triggered.", Toast.LENGTH_SHORT).show();
}
}
Here's popup_layout.xml per request. Thanks.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#drawable/customborder">
<TableRow>
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment3" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Species:" />
<EditText
android:id="#+id/textboxSpeciesxml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:text="(table is empty)"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btnSaveRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SAVE" />
<Button
android:id="#+id/btnUpdateRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="UPDATE" />
<Button
android:id="#+id/btnDeleteRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="DELETE" />
<Button
android:id="#+id/btnClearFormxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="CLEAR" />
</TableRow>
<TableRow>
<Button
android:id="#+id/btnFirstRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/pszFirstRecordButton"/>
<Button
android:id="#+id/btnPreviousRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/pszPreviousRecordButton"/>
<Button
android:id="#+id/btnNextRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/pszNextRecordButton"/>
<Button
android:id="#+id/btnLastRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/pszLastRecordButton"/>
</TableRow>
<TableRow>
<Button
android:id="#+id/btnPurgeTablexml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Purge Table!" />
<Button
android:id="#+id/btnDismissxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="3"
android:text="Dismiss" />
</TableRow>
</TableLayout>
If btnDismissxml and btnSaveRecordxml are your buttons in popup window, you could find them using layout.findViewById rather than MainActivity.this.findViewById. Use the following code:
View layout = (TableLayout) layoutInflater.inflate(R.layout.popup_layout, null);
// ...
btnDismiss=(Button) layout.findViewById(R.id.btnDismissxml);
btnDismiss.setOnClickListener(new OnClickListener() {
// ...
});
btnSaveRecord=(Button) layout.findViewById(R.id.btnSaveRecordxml);
btnSaveRecord.setOnClickListener(new OnClickListener() {
// ...
});

Custom action bar items listener

I create custom view for action bar: action_bar_bets.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:weightSum="2"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/actionBarProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="10dp"
android:src="#drawable/ic_action_user" />
<ImageButton
android:id="#+id/actionBarBets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="10dp"
android:src="#drawable/ic_action_betslip" />
</LinearLayout>
here is my menu: action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/mybetsCount"
android:actionLayout="#layout/action_bar_bets"
android:showAsAction="always"/>
</menu>
I create action bar in code:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
View view = getLayoutInflater().inflate(R.layout.action_bar_bets, null);
actionBar.setCustomView(view)
I tried set OnClickListener to ImageButtons:
like this:
ImageButton profile = (ImageButton) view.findViewById(R.id.actionBarProfile);
profile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Profile", Toast.LENGTH_SHORT).show();
}
});
and like this:
actionBar.getCustomView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.actionBarProfile:
Toast.makeText(getApplicationContext(), "Profile", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
But nothing happened
Can you help me to fix this problem?
ADD
Also in this class i have ListNavigation
PackageManager pm = getPackageManager();
String label;
try {
ActivityInfo activityInfo = pm.getActivityInfo(getComponentName(),
0);
label = activityInfo.loadLabel(pm).toString();
} catch (NameNotFoundException e) {
label = null;
e.printStackTrace();
}
String[] navigations = { label, "Sports", "Profile" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getBaseContext(), R.layout.custom_spinner_title_bar,
android.R.id.text1, navigations);
adapter.setDropDownViewResource(R.layout.custom_spinner_title_bar);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition,
long itemId) {
switch (itemPosition) {
case 1:
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
break;
case 2:
intent = new Intent(getApplicationContext(),
Activity_profile.class);
startActivity(intent);
break;
default:
break;
}
return false;
}
};
actionBar.setListNavigationCallbacks(adapter, navigationListener);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
return true;
}
im not sure where you initialize your buttons, but when it is in something like onCreate() then replace
ImageButton profile = (ImageButton) view.findViewById(R.id.actionBarProfile);
with
ImageButton profile = (ImageButton) findViewById(R.id.actionBarProfile);
Try this. This is example with a Switch but it's the same way for other component
getMenuInflater().inflate(R.menu.export, menu);
MenuItem item = menu.findItem(R.id.myswitch);
Switch switchOnActionBar = (Switch)item.getActionView().findViewById(R.id.switchForActionBar);
switchOnActionBar.setOnCheckedChangeListener(this);
SwitchForActionBar is in switch_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<Switch
android:id="#+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Manuel"
android:textOn="Auto"
android:gravity="center_vertical"
android:thumb="#drawable/switch_thumb"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:background="#android:color/transparent"
android:layout_marginRight="10dp"/>
</RelativeLayout>
And this is the menu layout
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/myswitch"
android:title=""
android:checkable="true"
android:showAsAction="always"
android:actionLayout="#layout/switch_layout"
/>
</menu>
try ontouchlistener for imagebutton :
Change
setOnClickListener(new OnClickListener()
to :
setOnTouchListener(new OnTouchListener()
I found where i has error.
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
actionBar.setCustomView(view, lp);
and delete this code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
return true;
}
My navigation list overlaps the buttons
Thank you all for the help

Categories

Resources