How to make a fixed BottomNavigationView with title below, no shifted navigation - java

I am having difficulties in making fixed BottomNavigationView like in the Photo below: OriginalPhoto.png
Here is my own project's BottomNavigationView: MyPhoto.png
Also, I want the title below icons visible all the time, not just when clicked. How can I do this??
Note: I have read all stackoverflow articles and followed other external links but no tangible results have gained. Please help me to figure this subtle problem out. Would be better if only with XML to solve this problem, rather than java code.
Here is my source code for activity_menu.xml, btw, it is not activity_main.xml because i have used main activity for my login page. This activity is after login page: `
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="datasite.com.konnex.Menu"
android:background="#ffffff">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3fc0ea">
<ImageButton
android:layout_width="120dp"
android:layout_height="38dp"
android:background="#drawable/lg1"
android:layout_marginLeft="130dp"
android:layout_marginStart="130dp"/>
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"
android:layout_marginStart="90dp"/>
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2">
<FrameLayout
android:layout_width="191dp"
android:layout_height="150dp"
android:id="#+id/fm_cases"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/cases"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="35dp"
android:id="#+id/img_cases" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cases"
android:layout_marginLeft="70dp"
android:layout_marginStart="70dp"
android:layout_marginTop="128dp"
android:textSize="18sp"
android:textColor="#424242"/>
<TextView
android:layout_width="22dp"
android:layout_height="wrap_content"
android:text="#string/string_1"
android:textColor="#FFFFFF"
android:textSize="17sp"
android:layout_marginLeft="122dp"
android:layout_marginStart="122dp"
android:layout_marginTop="35dp"
android:background="#E91E63"
android:id="#+id/notif_cases"/>
</FrameLayout>
</GridLayout>
</RelativeLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fffafa"
app:menu="#menu/navigation"
app:itemIconTint="#color/dark"
app:itemTextColor="#color/dark"
android:animateLayoutChanges="false"
android:splitMotionEvents="false"
android:fitsSystemWindows="true"/>
</LinearLayout>`
Here is the source code for menu.java. it is also after main.java activity which is for login used.
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class Menu extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_about:
return true;
case R.id.nav_location:
return true;
case R.id.nav_phone:
return true;
case R.id.nav_home:
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
FrameLayout fml = (FrameLayout) findViewById(R.id.fm_cases);
fml.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Menu.this, Cases.class);
startActivity(i);
}
});
}
}
Thank you in advance!!! I really need your help Guys)
My Navigation.xml:
`<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_about" />
<item
android:id="#+id/nav_location"
android:icon="#drawable/nav_location"
android:title="#string/title_location"
/>
<item
android:id="#+id/nav_phone"
android:icon="#drawable/nav_call"
android:title="#string/title_phone"
/>
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home"
/>
</menu>

Try using AHBottomNavigation library, I suggest this because is has simple implementation considering your case and reduces your work to create another menu.xml file and handling menu option methods in your code. Just add this line in your gradle file.
compile 'com.aurelhubert:ahbottomnavigation:2.0.6'
It will take care of the layout issue you have moreover, it has lots of customization options. You can read further here, but for your case the simple use to keep titles is..
public class MyActivity extends AppCompatActivity{
#BindView(R.id.bottom_navigationbar)
AHBottomNavigation bottomNavigation;
onCreate(){
bottomNavigation.addItem(new AHBottomNavigationItem("Title1", iconID1);
bottomNavigation.addItem(new AHBottomNavigationItem("Title2", iconID2);
bottomNavigation.addItem(new AHBottomNavigationItem("Title3", iconID3);
bottomNavigation.addItem(new AHBottomNavigationItem("Title4", iconID4);
bottomNavigation.setAccentColor(ContextCompat.getColor(this, themeColor));
//will always show titles
bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
}}
setUpClickListener.
bottomNavigation.setOnTabSelectedListener((position, wasSelected) -> {
if (position == 0 && !wasSelected) {
} else if (position == 1 && !wasSelected) {
} else if (position == 2 && !wasSelected) {
} else if (position == 3 && !wasSelected) {
} else if (position == 4 && !wasSelected) {
}
return true;
}
);

Ok then try to add app:showAsAction in menu item
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_dashboard_black_24dp"
app:showAsAction="ifRoom"
android:title="#string/title_about" />
<item
android:id="#+id/nav_location"
android:icon="#drawable/nav_location"
app:showAsAction="ifRoom"
android:title="#string/title_location"
/>
<item
android:id="#+id/nav_phone"
android:icon="#drawable/nav_call"
app:showAsAction="ifRoom"
android:title="#string/title_phone"
/>
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"
app:showAsAction="ifRoom"
android:title="#string/title_home"
/>
</menu>
If your problem not resolve then replace this one showAsAction="always|withText"
after then you can use this method for disabling shifted menu
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
Updated Menu.java: `package datasite.com.konnex;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.reflect.Field;
public class Menu extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_about:
return true;
case R.id.nav_location:
return true;
case R.id.nav_phone:
return true;
case R.id.nav_home:
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
//Add This
disableShiftMode((BottomNavigationView)findViewById(R.id.navigation))
FrameLayout fml = (FrameLayout) findViewById(R.id.fm_cases);
fml.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Menu.this, Cases.class);
startActivity(i);
}
});
}
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
}
Updated Navigation.xml:
`<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_about"
app:showAsAction="always|withText"/>
<item
android:id="#+id/nav_location"
android:icon="#drawable/nav_location"
android:title="#string/title_location"
app:showAsAction="always|withText" />
<item
android:id="#+id/nav_phone"
android:icon="#drawable/nav_call"
android:title="#string/title_phone"
app:showAsAction="always|withText"/>
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home"
app:showAsAction="always|withText" />
</menu>
`

Related

Lottie Animation not working with setExitSharedElementCallback android

I am working on Android with a Lottie animation. I implemented a shared transition on Lottie from one activity to another activity. But this is not Working. What am I doing wrong in my setExitSharedElementCallback method?
MainActivity.java
package com.example.trans;
import android.app.ActivityOptions;
import android.app.SharedElementCallback;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.airbnb.lottie.LottieAnimationView;
public class MainActivity extends AppCompatActivity {
LottieAnimationView lottieAnimationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lottieAnimationView = findViewById(R.id.animation_logo);
setExitSharedElementCallback(new SharedElementCallback() {
#Override
public Parcelable onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix, RectF screenBounds) {
int bitmapWidth = Math.round(screenBounds.width());
int bitmapHeight = Math.round(screenBounds.height());
Bitmap bitmap = null;
if (bitmapWidth > 0 && bitmapHeight > 0) {
Matrix matrix = new Matrix();
matrix.set(viewToGlobalMatrix);
matrix.postTranslate(-screenBounds.left, -screenBounds.top);
bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.concat(matrix);
sharedElement.draw(canvas);
}
return bitmap;
}
});
findViewById(R.id.start_animation).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, lottieAnimationView, "simple_activity_transition");
startActivity(intent, options.toBundle());
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context=".MainActivity">
<Button
android:id="#+id/start_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/animation_logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="click Me" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/animation_logo"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:transitionName="simple_activity_transition"
app:lottie_autoPlay="true"
app:lottie_loop="false"
app:lottie_rawRes="#raw/dm"
app:lottie_renderMode="hardware"
app:lottie_speed="1.2"
tools:ignore="MissingConstraints" />
</RelativeLayout>
activityMain2.java
package com.example.trans;
import androidx.appcompat.app.AppCompatActivity;
import android.app.SharedElementCallback;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ImageView;
import com.airbnb.lottie.LottieAnimationView;
import java.util.List;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setEnterSharedElementCallback(new SharedElementCallback() {
#Override
public View onCreateSnapshotView(Context context, Parcelable snapshot) {
View view = new View(context);
view.setBackground(new BitmapDrawable((Bitmap) snapshot));
return view;
}
#Override
public void onSharedElementStart(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
ImageView sharedElement = findViewById(R.id.animation_logo);
for (int i = 0; i < sharedElements.size(); i++) {
if (sharedElements.get(i) == sharedElement) {
View snapshot = sharedElementSnapshots.get(i);
Drawable snapshotDrawable = snapshot.getBackground();
sharedElement.setBackground(snapshotDrawable);
sharedElement.setImageAlpha(0);
forceSharedElementLayout();
break;
}
}
}
private void forceSharedElementLayout() {
ImageView sharedElement = findViewById(R.id.animation_logo);
int widthSpec = View.MeasureSpec.makeMeasureSpec(sharedElement.getWidth(),
View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(sharedElement.getHeight(),
View.MeasureSpec.EXACTLY);
int left = sharedElement.getLeft();
int top = sharedElement.getTop();
int right = sharedElement.getRight();
int bottom = sharedElement.getBottom();
sharedElement.measure(widthSpec, heightSpec);
sharedElement.layout(left, top, right, bottom);
}
#Override
public void onSharedElementEnd(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
ImageView sharedElement = findViewById(R.id.animation_logo);
sharedElement.setBackground(null);
sharedElement.setImageAlpha(255);
}
});
}
}
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#000"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/animation_logo"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:transitionName="simple_activity_transition"
app:lottie_autoPlay="true"
app:lottie_loop="false"
app:lottie_rawRes="#raw/dm"
app:lottie_renderMode="hardware"
app:lottie_speed="1.2" />
</RelativeLayout>
Styles
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/explode</item>
</style>

How can I add drawerLayout and not to damage existed LinerLayout style?

I began doing a project without DrawableLayout. I am using mostly LinearLayout. I want to add drawer navigation android, and as I know, I need DrawableLayout.
How can I add this, and not damage everything I have now?
Well don't be afraid. It's not so difficult. Just follow the following steps to get it:-
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_dark">
<!-- put you linear layout here --!>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
android:id="#+id/nv">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Navigation Header:
<?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="wrap_content"
android:gravity="center"
android:background="#color/colorPrimaryDark">
<ImageView
android:layout_width="180dp"
android:layout_height="130dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:background="#drawable/ic_android_black_24dp"/>
</LinearLayout>
**Create a menu for the navigation drawer**
Right-click the res folder →Select new →Android resource file →Choose ‘menu’ under the resource type drop-down list.
Name the file as ‘navigation_menu.xml’ and copy-paste the following code into the file.
`<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/account"
android:icon="#drawable/ic_portrait_black_24dp"
android:title="My Account"/>
<item android:id="#+id/settings"
android:icon="#drawable/ic_brightness_high_black_24dp"
android:title="Settings"/>
<item android:id="#+id/mycart"
android:icon="#drawable/ic_local_grocery_store_black_24dp"
android:title="My Cart"/>
</menu>
** Lastly, in you activity:** <br>
In this last and final step, we will write Java code in the MainActivity.java file.
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private DrawerLayout dl;
private ActionBarDrawerToggle t;
private NavigationView nv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dl = (DrawerLayout)findViewById(R.id.activity_main);
t = new ActionBarDrawerToggle(this, dl,R.string.Open, R.string.Close);
dl.addDrawerListener(t);
t.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
nv = (NavigationView)findViewById(R.id.nv);
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch(id)
{
case R.id.account:
Toast.makeText(MainActivity.this, "My Account",Toast.LENGTH_SHORT).show();break;
case R.id.settings:
Toast.makeText(MainActivity.this, "Settings",Toast.LENGTH_SHORT).show();break;
case R.id.mycart:
Toast.makeText(MainActivity.this, "My Cart",Toast.LENGTH_SHORT).show();break;
default:
return true;
}
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(t.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
}`

I want to hide and unhide text using a button in android ; The code is correct but it still doesn't work

Here is the xml file
<?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:id="#+id/activity_hide"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.andrew.hide.Hide">
<TextView
android:text="This is the text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="81dp"
android:id="#+id/textView"
android:textSize="40sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Hide"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/button"
android:textSize="28sp"
android:onClick="hide"
android:layout_marginBottom="184dp" />
</RelativeLayout>
Here is the java file
package com.example.andrew.hide;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Hide extends Activity {
Button b1;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hide);
b1=(Button) findViewById(R.id.button);
t1=(TextView) findViewById(R.id.textView);
String text=b1.getText().toString();
}
public void hide(View view) {
if (b1.getText().toString() == "Hide") {
t1.setVisibility(View.INVISIBLE);
b1.setText("Unhide");
}
if (b1.getText().toString() == "Unhide") {
t1.setVisibility(View.VISIBLE);
b1.setText("Hide");
}
}
}
I am new to android development , so ignore if this doubt is silly.
I manually checked by using Log and I found that getText works perfectly
Nothing happens whenever i click the button (b1 here),How do I fix this?
Use equals to compare strings.
Try this,
public void hide(View view) {
String text = b1.getText().toString();
if (text.equals("Hide")) {
t1.setVisibility(View.INVISIBLE);
b1.setText("Unhide");
} else if (text.equals("Unhide")) {
t1.setVisibility(View.VISIBLE);
b1.setText("Hide");
}
}
Why don't you simply check the TextView visibility and perform your actions based on that ? It's all the setters and getters right ?
b1.onClickListener(new OnClickListener() {
if (t1.getVisibility() == View.Visible) {
t1.setVisibilty(View.GONE);
//--- your code
} else {
t1.setVisibilty(View.VISIBLE);
//--- your code
}
);

How to implement image with text in Menu options in android

i am trying so hard to implement customized menu option with both image and text in menu item list but i did not get it...so please help me how to implement it...thank you in advance
here is my menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.admin.sst.Signuplogin">
<item android:id="#+id/one" android:title="one"
app:showAsAction="never"
/>
<item android:id="#+id/two" android:title="two"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/three" android:title="three"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
You can use PopupMenu on any view clicked.
Full Demo:
MainActivity:
package pk.sohail.gallerytest.activity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
import java.lang.reflect.Field;
import pk.sohail.gallerytest.R;
public class MainActivity extends Activity {
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(context, view);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.one:
Toast.makeText(getApplicationContext(), "1",
Toast.LENGTH_SHORT).show();
return true;
case R.id.two:
Toast.makeText(getApplicationContext(), "2",
Toast.LENGTH_SHORT).show();
return true;
case R.id.three:
Toast.makeText(getApplicationContext(), "3",
Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
popupMenu.inflate(R.menu.main);
// Force icons to show
Object menuHelper;
Class[] argTypes;
try {
Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
fMenuHelper.setAccessible(true);
menuHelper = fMenuHelper.get(popupMenu);
argTypes = new Class[]{boolean.class};
menuHelper.getClass()
.getDeclaredMethod("setForceShowIcon", argTypes)
.invoke(menuHelper, true);
} catch (Exception e) {
// Possible exceptions are NoSuchMethodError and
// NoSuchFieldError
//
// In either case, an exception indicates something is wrong
// with the reflection code, or the
// structure of the PopupMenu class or its dependencies has
// changed.
//
// These exceptions should never happen since we're shipping the
// AppCompat library in our own apk,
// but in the case that they do, we simply can't force icons to
// display, so log the error and
// show the menu normally.
Log.w("as", "error forcing menu icons to show", e);
popupMenu.show();
return;
}
popupMenu.show();
}
});
}
}
activity_main.xml:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pop Menu"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
R.menu.main:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.admin.sst.Signuplogin">
<item
android:id="#+id/one"
android:icon="#mipmap/ic_launcher"
android:title="one"
app:showAsAction="never" />
<item
android:id="#+id/two"
android:icon="#mipmap/ic_launcher"
android:orderInCategory="100"
android:title="two"
app:showAsAction="never" />
<item
android:id="#+id/three"
android:icon="#mipmap/ic_launcher"
android:orderInCategory="100"
android:title="three"
app:showAsAction="never" />
</menu>

How to change imageview shape to round on menu item selected in android?

I am new to android and now I am stuck at one point.
i want to change shape of imageview when user click on menu item Circle.
my circle_shape.xml file in drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomRightRadius="10dp"/>
<solid android:color="#FF44FF"/>
</shape>
my java File Shape.java
package com.example.mixapp;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
public class Shape_with_Spinner extends Activity {
Spinner shapeSpinner;
ImageView imgMaster;
private Drawable drawable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shape_with__spinner);
try {
imgMaster = (ImageView) findViewById(R.id.imgMaster);
Toast.makeText(this, "in try part ", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.i("Magaj jay che have", ""+e.getLocalizedMessage());
Toast.makeText(this, "in catch part : "+e.getMessage(), Toast.LENGTH_LONG).show();
}
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.rect:
drawable = getDrawable(R.drawable.circle_shape);
break;
default:
break;
}
return super.onMenuItemSelected(featureId, item);
}
}
and my Shape.xml file in layout folder.
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.mixapp.Shape_with_Spinner" >
<Spinner
android:id="#+id/shapeSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:entries="#array/Spinner_value"/>
<ImageView
android:id="#+id/imgToShape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
any help would be appreciable. :)
I would recommend you to use Picasso or Glide for it. This libs not only allowed you to async load images, but you could also set up the transformations(here is ready to use circle transformations Picasso, Glide) for any image that should be loaded.

Categories