Action Bar Buttons position - java

I am trying to put a button in the action bar instead of the application icon which means at left. I have tried orderInCategory but it doesn't help.
What is the right way to do that?

If you just want to change the icon of the "home" section of the Action Bar, you can use ActionBar.setIcon() programatically, or set android:icon in your XML. If you want to do something more complex than that (have multiple buttons, or be able to customize them further), you need to set a custom view in your action bar.
To set a custom view in your action bar, you need to do the following:
Create a layout for your custom view:
<LinearLayout
android:width="wrap_content"
android:height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/button1"
android:width="wrap_content"
android:height="wrap_content"
android:src="#drawable/button1" />
</LinearLayout>
And then inflate it and set it as your action bar's custom view:
LayoutInflater inflater = LayoutInflater.from(context);
View customView = inflater.inflate(R.layout.custom_view, null);
ImageView button = (ImageView) customView.findViewById(R.id.button1);
button.setOnClickListener(...);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(customView);
Hope that helps

Related

How to Scroll Down Linear Layout When linearLayout is visible on button click [kotlin]

I have bunch of text view and edit text form inside a scroll view and also having a linear Layout below form inside same scroll view in this linear layout having bunch of text view vertical. Initially linear layout has visibility gone when user press "add more info",which is aling just above this linear layout, button then layout visibility is Visible. This is working fine as expected but what i want is when linear layout get visible then scroll view should scroll down so that user can see the extended data. I tried all but nothing can help me.
Please help me to solve this.Below is ShowHideLayout method.
Need Help
fun showHideLayout() {
if (linearLayoutAddMoreInfoExpand.visibility == GONE) {
linearLayoutAddMoreInfoExpand.visibility = VISIBLE
mTextViewAddMoreInfo.setText("Close Add More Info")
scrollView.post(Runnable {
scrollView.fullScroll(ScrollView.FOCUS_DOWN)
//scrollView.scrollTo(0,scrollView.bottom)
})
// scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
scrollView.scrollTo(0,scrollView.bottom)
//scrollView.smoothScrollBy(R.id.linearLayoutAddMoreInfoExpand,R.id.scrollBottom)
} else if (linearLayoutAddMoreInfoExpand.visibility== VISIBLE) {
linearLayoutAddMoreInfoExpand.visibility = GONE
mTextViewAddMoreInfo.setText("+ Add More Info")
}
}
You can solve this adding another scrollview before your exapandLinearLayout in xml file and call this scrollview on button click so your whenever your linear layout is it will scroll down. But it may not be best practice.
Your code will be like below
<Scrollview
android:id = #+id/scrollViewLayout
.....
....>
<LinearLayout....>
<--Remaining view-->
</LinearLayout>
</Scrollview>
And in Java on addmoreinfo button click call this scrollview like below:-
fun showHideLayout() {
if(linearLayoutAddMoreInfoExpand.visibility == GONE){
linearLayoutAddMoreInfoExpand.visibility = VISIBLE
mTextViewAddMoreInfo.setText("Close Add More Info")
scrollViewLayout.post(Runnable { scrollView.fullScroll(ScrollView.FOCUS_DOWN) })

Get Menu Group as View

I'm using the MaterialShowcaseView to give users an overview of my app. set a target for the showcase, it must be a View. Below is an example of a sequence:
String SHOWCASE_ID = getString(R.string.showcase_id);
ShowcaseConfig config = new ShowcaseConfig();
config.setDelay(500); // half second between each showcase view
MaterialShowcaseSequence sequence = new MaterialShowcaseSequence(this, SHOWCASE_ID);
sequence.setConfig(config);
...
sequence.addSequenceItem(
new MaterialShowcaseView.Builder(this)
.setTarget(findViewById(R.id.navigation_header))
.setDismissText(getString("GOT IT"))
.setTitleText(getString(R.string.onboarding_navigation_header_title))
.setContentText(getString(R.string.onboarding_navigation_header_content))
.withRectangleShape(false)
.build());
...
sequence.start();
In this example, I'm targeting the navigation header of the following NavigationView:
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/main_navigation"
app:headerLayout="#layout/nav_header"/>
How would I get the menu in the snippet above as a View so I can pass that intosetTarget() for my showcase sequence?
I have tried using just the NavigationView, but that targets both the header layout and the menu. I have also tried getting a group of the menu by ID (findViewById(R.id.menu_group_id)), but I don't believe the menu group is technically a View. I can get the menu via NavigationView.getMenu(), but that returns a Menu object. Any ideas?
Edit: The entire menu needs to be targeted. On a side note,this NavigationView exists in a DrawerLayout, if that changes anything.
There is no direct way to get the view reference of menu group in NavigationView. You could remove or hide the header view at runtime when targets NavigationView, and add or show the header view again when targets other.
you could use getheaderview to get the header and call:
your_header.setVisibility(View.GONE) //hide
your_header.setVisibility(View.VISIBLE) //show
You can access the menu items by using them as action views. Set the actionViewClass in your menu.xml like so:
<item
...
app:actionViewClass="android.support.design.widget.NavigationView"
/>
And then in your activity:
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
...
sequence.addSequenceItem(
new MaterialShowcaseView.Builder(this)
.setTarget(findViewById(R.id.navigation_header))
.setDismissText(getString("GOT IT"))
.setTitleText(getString(R.string.onboarding_navigation_header_title))
.setContentText(getString(R.string.onboarding_navigation_header_content))
.withRectangleShape(false)
.build());
...
return true;
}

I would like to add multiple Strings to my android Button programmatically (not through XML)

I'm trying to format the three Strings into an android button. The first string should be about 30% of the button, second should be about 50%, and the rest to the third button. Each text should be contrained within a certain length. How can I do this?
After searching through many possible solutions, what I came up with was to create 3 different buttons and put them together. This wouldn't be ideal but it's a start. Are there better ways to do this?
This is what I have tried so far:
The Java:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout_buttons);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Button addButton = new Button(this);
Button numPlayerButton = new Button(this);
Button lobbyButton = new Button(this);
Button locationButton = new Button(this);
ll.addView(addButton, lp);
ll.addView(numPlayerButton, lp);
ll.addView(lobbyButton, lp);
ll.addView(locationButton, lp);
XML:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearlayout_buttonlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearlayout_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- THIS IS WHERE THE BUTTONS GO!!!!!! -->
</LinearLayout>
</LinearLayout>
</ScrollView>
My goal with this code was to have the buttons created horizontally with the inner linear layout, and I thought the outer linear layout will create the next iteration of buttons on the next line (vertically). I have the buttons created programmatically through a loop.
If you want to click a widget then do something(as a button does), you can use a Layout and add a onClickListener to it. In this Layout, you can add strings as you want.
Simply do it like that:
Write a RelativeLayout in your XML and give it a id.
Add three textViews in RelativeLayout and place them as you want.
Register this RelativeLayout in Java and add a onClickListener to it.
For Java part:
RelativeLayout ThreeStringLayout = (RelativeLayout) view.findViewById(R.id.three_string_layout);
ThreeStringLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view1) {
//do things here
}
});
I implemented it in a Fragment, you need to modify it if you use it in Activity.
Good Luck.
This cannot be achieved using a standard Android Button (that I know of). You will have to use a linear layout with textviews. I can explain how that would work if need help

convert android UI xml to code

So, I'm trying to make a dynamic UI, and i want to add a seperator to it. unfortunately, i could only find out how do one in XML. is it possible to turn this
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/seperator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:scaleType="fitXY"
android:src="#android:drawable/divider_horizontal_dark" />
into program code?
my best attempt was
ImageView seperator=new ImageView(this);
seperator.setImageDrawable(drawable.divider_horizontal_dark);
Put it in an extra layout file and inflate it when you need it in code - I think that what you want to do and should be the easiest way.
In your Activity:
View v = LayoutInflater.from(this).inflate(R.layout.seperator, null);
If you inflate a Layout:
LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_layout, null);
TextView tv = (TextView) ll.findViewById(R.id.tv);
There is a website can convert that for you. You can design the interface with eclipse then submit the generated xml to XMLtoJAVA online converter and it should do it for you..
You can also create a View, define a background and add it with a LayoutParams
ViewGroup container = (ViewGroup) findViewById(R.id.container);
View separator = new View(context);
separator.setBackgroundColor(Color.Black);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 2);
container.addView(separator, layoutParams);

findViewById on Custom Title Bar within Tab Activity

I have a custom title bar set on my TabActivity. The custom title bar contains a TextView and an ImageView in it. The tabHost has multiple tabs.
What I want to do is to access the ImageView resource in the tabs.
I am accessing the TextView from custom title bar in the main Tab activity (that extends TabActivity) and it is working fine. Following is the code which is working fine in main activity:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.myactivity);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mycustomtitle);
TextView tv = (TextView) findViewById(R.id.viewTitleId);
Now I want to access the ImageView in my tabs (which are added in tabHost). If I set following code in tab:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
It gives following error:
You cannot combine custom titles with other title features
And if i directly set following in the tab code:
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mycustomtitle);
ImageView image = (ImageView) findViewById(R.id.imageTitleId);
The image remains null.
mycustomtitle.xml has following two elements:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.80"
android:id="#+id/viewTitleId"
android:textColor="#color/titleBarTextColor"
android:gravity="center_vertical"
android:text="#string/viewText"
android:textSize="18px"
android:paddingLeft="45px"
/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.20"
android:src="#drawable/btn_save"
android:id="#+id/imageTitleId"
>
</ImageView>
Please give me some idea how can I access the Custom title's ImageView in the tabs ?
You can access the TextView and ImageView by declaring them public static in the TabActivity. Then, in the Sub-Activity you obviously access the public static TextView and ImageView like,
Main_Tab_Activity.textView.setText("my_text_view");
If you put Custom Title bar only in Main TabActivity that Title bar will appears in all of your Sub-TabActivity.
For Example if you gave Custom title Bar in Main TabActivity :
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.header_layout);
TextView tv = (TextView) findViewById(R.id.viewTitleId);
ImageView image = (ImageView)findViewById(R.id.imageTitleId);
there is no need to give in all you Sub-tab Activity.
In all sub-Tab Activity you just set setContentView(R.layout.sub_layout);
use
getParent().getWindow().findViewById(id)
insted id findViewById(id);
for access any view of parent.

Categories