8

Hi I am New To Android and I Want to Change the Textview Size and Textview Color of a Activity from Menu options I don't know how to Do it

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 Display Option from the current layout Menu option a new layout want to be called.. in that layout I have created Two Button one is Plus Button and other one is Minus Button and I want these buttons to make changes to the previous layout.

When I Click the plus button the text view text want to be Change and when I click Minus Button the Text view Size was Reduced.

The Custom layout Code when Menu Item was Clicked

<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 have a Textview)

@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 app and author as this App Crash When Trying to Change textsize in textView by clicking on button

Community
  • 1
  • 1

5 Answers5

6

Display the new layout in the form of a DialogFragment. The idea is to show the increase/Decrease option in the form of a DialogFragment popup. you can increase/decrease size from the popup. Once the popup dismisses the selected size is set to the 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>
sarath19
  • 198
  • 1
  • 11
  • i ACCEPTED yOUR aNSWER bUT aFTER A cERTAIN LIMIT THE PLUS MINUS DID A MINUS BUTTON WORK (ie) after a 5 to 6 click of plus button the font size was start decreasing and also the same way minus button also did @sarath19 –  Nov 05 '15 at 09:54
  • I just place a 4 Button Red,green,blue the same way i want to change the Textview color so how to do it? –  Nov 05 '15 at 12:23
  • `color= sizeOfText.getCurrentTextColor(); if (id==R.id.red) { //What to do Here? sizeOfText.setTextColor(this.getResources().getColor(R.color.red)); }` @sarath19 –  Nov 05 '15 at 13:14
  • JI please Check this and Help http://stackoverflow.com/questions/33672519/need-to-display-image-image-name-is-stored-as-text-in-sqlite-but-images-in-ser –  Nov 16 '15 at 10:34
2

Sometimes I trace the flow of a user's activity to get the full picture of what they are trying to achieve and, often, the design choices they make are based on answers they receive here, and so the nest question is based on the solution provided in the previous question. Sometimes this is good, other times the person does not get a good grasp of the design options available to them.

So to answer the question:
There are many ways to do this (my list is not exhaustive), I am suggesting a few and demonstrating one.

You could use:

  1. fragments
  2. shared preferences
  3. intents
  4. have the resize options within the menu

I've chosen to demonstrate the fragments, as it is useful for many other things and helpful skill to have.

Create a fragment with your plus and minus buttons, rather than a separate activity. This way the fragment has direct access to the methods of the attached activity.

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

Within your activity:

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.
}

Within 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 a Fragment:

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's activity's 
    // 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 programming design decisions, there can be many correct solutions, some are better than others, some are just a matter of preferred design choice. Until we have the options, we have no choice.

  • Thanks For The Detail Explanation @Yvette –  Nov 05 '15 at 10:03
  • 1
    Ya You Did a Big Help For Us We are Proud and happy to get a Member friend like u on stackoverflow thanks a Lot We Blessed @Yvette –  Nov 05 '15 at 10:08
1

Try

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (id==R.id.display_option)
    {
        setContentView(R.layout.YOUR_LAYOUT)
    }


    return super.onOptionsItemSelected(item);
}
Prakhar
  • 710
  • 6
  • 24
  • App was Stop and display android.view.InflateException: Binary XML file line #16: Error inflating class in degugger –  Nov 02 '15 at 12:35
1
    @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;
}
sarath19
  • 198
  • 1
  • 11
  • my View Group is **Relative Layout** !! so in **myCustomLayout()** can i change LinearLayout to **RelativeLayout**?? @sarath19 –  Nov 04 '15 at 12:18
  • replace getActivity() with "this" keyword – sarath19 Nov 04 '15 at 13:24
  • can i replace parentView as object name of relative layout? –  Nov 04 '15 at 13:40
  • `RelativeLayout relativeLayout= new RelativeLayout(this); View customView = LayoutInflater.from(this).inflate(R.layout.custom_setting, null, false); relativeLayout.addView(customView); parentView.addView(relativeLayout); ` and i got the error in logcat java.lang.StackOverflowErrorat android.view.View.isLayoutDirectionInherited(View.java:13306) @sarath19 –  Nov 04 '15 at 13:49
0

Try this.Hopefully it 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);

    }    
  }
Naveen
  • 814
  • 2
  • 9
  • 22
  • hoW TO CALL A LAYOUT IN OnOptionItemSelected(MenuItem item) @Naveen –  Nov 02 '15 at 12:26
  • You dont call alayout, you just update the layout. The one you referenced in the onCreate(). – Skynet Nov 02 '15 at 12:27
  • HOw?? in my activity i have a menu option"DisplayOption" When i CLick that i want to Increment the textview Size and Button @Skynet –  Nov 02 '15 at 12:35