How to format activity from another event using menu options

Hi, I am new to Android and I want to change the text size and text image. Activity color from menu options. I do not know how to do that.

I created a menu item like this

<item android:id="@+id/menu_item_share" android:title="Share" android:showAsAction="ifRoom|withText" android:orderInCategory="100" app:actionProviderClass="android.support.v7.widget.ShareActionProvider" /> <item android:id="@+id/display" android:orderInCategory="100" android:showAsAction="ifRoom|withText" android:title="Display Option" android:actionLayout="@layout/custom_setting" /> 

When I click the Screen option from the current layout menu option, the new layout wants to be called up .. in this layout I created Two buttons , one Plus button and the other Minus button , and I want these buttons to make changes to the previous layout .

When I click the plus button, the text of the text view wants to be Change, and when I click the Minus button, the size of the Text Size is reduced.

Custom code layout when you click a menu item

 <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="com.example.aeiltech.sidd.CustomSetting" android:background="@mipmap/bg"> <ImageButton android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/close" android:layout_above="@+id/textView" android:layout_toRightOf="@+id/minus" android:layout_toEndOf="@+id/minus" android:id="@+id/close" /> <ImageButton android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/plusign" android:layout_marginTop="110dp" android:id="@+id/plus" android:layout_alignParentTop="true" android:layout_alignLeft="@+id/textView" android:layout_alignStart="@+id/textView" /> <ImageButton android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/minusign" android:id="@+id/minus" android:layout_alignTop="@+id/plus" android:layout_alignRight="@+id/textView" android:layout_alignEnd="@+id/textView" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Change Text Size" android:id="@+id/textView" android:layout_above="@+id/minus" android:layout_centerHorizontal="true" android:textColor="#FFFFFF" /> </RelativeLayout> 

DetailActivity (this activity has a textual representation)

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.detail_activity); detailtext= (TextView) findViewById(R.id.detail); dbHelper = new SqlLiteDbHelper(this); try { dbHelper.openDataBase(); } catch (SQLException e) { e.printStackTrace(); } sqLiteDatabase = dbHelper.getReadableDatabase(); cursor=dbHelper.getdetails(sqLiteDatabase, selectedData); if(cursor.moveToFirst()) { detailtext.setText(cursor.getString(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_detail, menu); MenuItem shareItem = menu.findItem(R.id.menu_item_share); mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (id==R.id.display_option) { LayoutInflater layoutInflater= (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); layoutInflater.inflate(R.layout.custom_setting,null); } return super.onOptionsItemSelected(item); } 

To help clarify, this question is the same application and author as this application error when trying to change text in text form by clicking

+8
android android-layout textview menuitem
source share
5 answers

Display the new layout as a dialog box. The idea is to show the zoom in / out option as a DialogFragment popup. You can zoom in / out from the popup. As soon as the popup is rejected, the selected size is set to TextView in your activity.

MainActivity.java

 public class MainActivity extends AppCompatActivity { public static int updatedSize; TextView greetingText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); greetingText = (TextView)findViewById(R.id.hello_world_text); } @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) { int id = item.getItemId(); if (id == R.id.action_settings) { mycustomLayout(); return true; } return super.onOptionsItemSelected(item); } public void mycustomLayout(){ FragmentManager manager = getFragmentManager(); PopUp popUp = new PopUp(new FragmentDismissHandler()); popUp.show(manager,"POPUP"); } public void refreshText(){ if( updatedSize>0) greetingText.setTextSize(updatedSize); } private class FragmentDismissHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); refreshText(); } } 

}

activity_main.xml

 <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=".MainActivity"> <TextView android:id="@+id/hello_world_text" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

PopUp.java (custom layout as DialogFragment)

 public class PopUp extends DialogFragment implements View.OnClickListener{ Button increase,decrease,set; TextView sizeOfText; static int size; Handler handler; public PopUp(Handler handler) { this.handler = handler; } public PopUp(){ } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment getDialog().setTitle("change text size"); View view = inflater.inflate(R.layout.my_custom_layout, container, false); increase = (Button)view.findViewById(R.id.inc); increase.setOnClickListener(this); decrease = (Button)view.findViewById(R.id.dec); decrease.setOnClickListener(this); sizeOfText = (TextView)view.findViewById(R.id.size_text); sizeOfText.setText("text"); set = (Button)view.findViewById(R.id.ok); set.setOnClickListener(this); size = (int) sizeOfText.getTextSize(); return view; } @Override public void onClick(View view) { int id= view.getId(); if(id==R.id.inc){ size = size +1; sizeOfText.setText("text"); sizeOfText.setTextSize(size); } if(id== R.id.dec){ size = size - 1; sizeOfText.setText("text"); sizeOfText.setTextSize(size); } if(id== R.id.ok){ dismiss(); } } @Override public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); MainActivity.updatedSize = size; handler.sendEmptyMessage(0); } 

}

my_custom_layout.xml

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white"> <Button android:id="@+id/inc" android:text="increase" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/dec" android:text="decrease" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/size_text" android:padding="20dp" android:textColor="@android:color/black" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/ok" android:text="set" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 
+6
source share

Sometimes I track the flow of user activity in order to get a complete picture of what they are trying to achieve, and often the choice of design that they make is based on the answers they get here, and therefore the nest question is based on the solution posed in the previous question. Sometimes it’s good, in other cases a person does not understand the available design options.

To answer the question:
There are many ways to do this (my list is not exhaustive), I offer several and demonstrate them.

You can use:

  • Fragments
  • General settings
  • intentions
  • have resize options in the menu

I decided to demonstrate the fragments, as this is useful for many other things and useful skill.

Create a snippet with your plus and minus buttons, not a separate action. Thus, the fragment has direct access to the methods of the applied activity.

Use your menu options to call the showButtonOptions () method of your activity.

As part of your business:

 Fragment fragment; FrameLayout frameLayout; @Override protected void onCreate(Bundle savedInstanceState) { ..../ View view = this.findViewById(android.R.id.My_Activity); frameLayout = (FrameLayout) findViewById(R.id.fragFrame); ..../ } public void ChangeMyLayoutMethod(args){ ..../ //use args to make change. } public void showButtonOptions() { // Create new fragment and transaction fragment = new MyPlusAndMinusButtonFragment(); fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.fragFrame, fragment); fragmentTransaction.commit(); frameLayout.setVisibility(View.VISIBLE); } public void returnToActivity() { frameLayout.setVisibility(View.GONE); // To do manage your back stack. } 

In your activity xml

 <RelativeLayout ... or whatever ...> .../ Details of that activity <FrameLayout android:id="@+id/fragFrame" android:layout_below="@+id/wherever" android:layout_height="your choice" android:layout_width="your choice" android:layout_marginTop=".. etc" android:visibility="gone"> </RelativeLayout> 

Create snippet:

 public class MyPlusAndMinusButtonFragment extends Fragment { FragmentManager fragmentManager; ..../ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.My_Plus_And_Minus_Button_Fragment, container, false); fragmentManager = getFragmentManager(); ...../ return view; } //To Do // Manage button clicks// onClick(){ // Call the parent activity // Cast this activity to type of parent activity. ((MyActivity) getActivity()).ChangeMyLayoutMethod(args); } 

Fragment Layout, My_Plus_And_Minus_Button_Fragment.xml:

 <FrameLayout .....> <RelativeLayout ....> <ImageButtons etc... ..../ </RelativeLayout> </FrameLayout> 

As with most software development decisions, there can be many correct decisions, some of them are better than others, some of them are just a matter of preferred design choice. As long as we have no options, we have no choice.

+2
source share

Try

 @Override public boolean onOptionsItemSelected(MenuItem item) { if (id==R.id.display_option) { setContentView(R.layout.YOUR_LAYOUT) } return super.onOptionsItemSelected(item); } 
+1
source share
  @Override public boolean onOptionsItemSelected(MenuItem item) { if (id==R.id.display_option) { myCustomLayout(); } return super.onOptionsItemSelected(item); } public void myCustomLayout(){ LinearLayout ll = new LinearLayout(getActivity()); ll.setOrientation(LinearLayout.VERTICAL); View customView = LayoutInflater.from(getActivity()).inflate(R.layout.my_custom_layout,null,false); ll.addView(customView); parentView.addView(ll); // parentView is a layout in your activity xml; } 
+1
source share

Try it. Hope this works.

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); MenuInflater inflater=getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } 

// OnOptionItemSelected

  @Override public boolean OnOptionItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.menu_item_share: Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show(); return true; default: return super.onOptionsItemSelected(item); } } 
0
source share

All Articles