setImageResource doesn't work in Android - java

I'm a beginner of java, I want to randomize few images, I use this code for that
int[] cards = {R.drawable.i1, R.drawable.i2, R.drawable.i3};
Random n = new Random();
Resources resources = getResources();
imgRandom.setImageResource(cards[n]);
and it doesn't work beacause setImageResource is red and the Android Studio says: cannot resolve symbol 'setImageResource'. Someone can help me, please?
All java code:
package eduard.alarmamotivationala;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
ImageView m_imgRandom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public ImageView getM_imgRandom() { int[] cards = {R.drawable.i1, R.drawable.i2, R.drawable.i3};
Random n = new Random();
Resources resources = getResources();
m_imgRandom.setImageResource(cards [3]);
return m_imgRandom; }
}
all XML code:
<?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_main"
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"
android:background="#000000"
tools:context="eduard.alarmamotivationala.MainActivity">
<Button
android:text="Porneste alarma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/button" />
<Button
android:text="Opreste alarma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:id="#+id/button2" />
<TextView
android:text="Inca nu ai setat alarma"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:id="#+id/textView" />
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.7"
android:scaleY="0.7"
android:headerBackground="#android:color/transparent"
android:numbersSelectorColor="#027aed"
android:id="#+id/timePicker"
android:layout_marginTop="36dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ImageView
android:id="#+id/imgRandom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType = "centerCrop" />
</RelativeLayout>

int[] cards = {R.drawable.i1, R.drawable.i2, R.drawable.i3};
int n = new Random().nextInt(3);
imgRandom.setImageResource(cards[n]);
To generate a random number from 0 to 2
int n = new Random().nextInt(3);
puck the random image from array and set to image
imgRandom.setImageResource(cards[n]);
Thats all !!

<?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_main"
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"
android:background="#000000"
tools:context="eduard.alarmamotivationala.MainActivity">
<Button
android:text="Porneste alarma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/button" />
<Button
android:text="Opreste alarma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:id="#+id/button2" />
<TextView
android:text="Inca nu ai setat alarma"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:id="#+id/textView" />
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.7"
android:scaleY="0.7"
android:headerBackground="#android:color/transparent"
android:numbersSelectorColor="#027aed"
android:id="#+id/timePicker"
android:layout_marginTop="36dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<!--changed wrap_content to match_parent for getting the full screen image view-->
<ImageView
android:id="#+id/imgRandom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType = "centerCrop" />
</RelativeLayout>
Also try,
android:scaleType = "centerInside"
and
android:scaleType = "fitXY"
feel the change.
And in java file,
package eduard.alarmamotivationala;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int[] cards = {R.drawable.i1, R.drawable.i2, R.drawable.i3};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing the image view
ImageView m_imgRandom = (ImageView) findViewById(R.id.imgRandom);
// choosing a random number from 0 to 2
int n = new Random().nextInt(3);
// choosing the random image from cards array using the random number
m_imgRandom.setImageResource(cards[n]);
}
}
Not tested.hope it works !!

Related

EditText autoscroll doesn't work after showing dialog

I'm developing an app for users to share their books. To achieve this, I'm getting different data about the book from the user. The problem is, horizontal autoscrolling of all EditTexts are working until Dialog has been shown and dismissed. Once a dialog has been dismissed on the fragment, horizontal autoscrolling of the EditText's on this layout won't work.
I added:
android:focusable="true"
android:focusableInTouchMode="true"
to parent layout of the edittext but doesn't work.
Also to make sure that autoscrolling is enabled I've added:
android:scrollHorizontally="true"
But none of above helped.
Here is XML code of the fragment layout.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical"
android:paddingStart="#dimen/dp25"
android:paddingEnd="#dimen/dp25"
tools:context=".UI.Fragments.SharePostFragments.Fragment1.OverViewFragment">
<LinearLayout
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<EditText
android:id="#+id/edit_text_name_of_book"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:fontFamily="#font/segoe_ui_semi_bold"
android:hint="#string/name_of_book"
android:inputType="text"
android:maxLength="50"
android:maxLines="1"
android:scrollHorizontally="true"
android:textAlignment="textStart"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="#dimen/font22" />
<TextView
android:id="#+id/text_view_number_of_characters"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="#dimen/dp10"
android:fontFamily="#font/segoe_ui_light"
android:text="#string/_0_50"
android:textColor="#color/colorAccent"
android:textSize="#dimen/font16"
tools:ignore="RtlSymmetry" />
<EditText
android:id="#+id/edit_text_name_of_author"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:layout_marginTop="#dimen/dp30"
android:background="#drawable/round_text_box_gray"
android:fontFamily="#font/segoe_ui_regular"
android:hint="#string/name_of_writer"
android:inputType="text"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<RelativeLayout
android:id="#+id/constraint_layout_1"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:fontFamily="#font/segoe_ui_regular"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/price_of_book"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/azn_sign"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<EditText
android:id="#+id/edit_text_price_of_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/textView2"
android:background="#color/white"
android:clickable="false"
android:hint="#string/_0_0"
android:inputType="text|numberDecimal"
android:maxLength="6"
android:singleLine="true"
android:textColorHint="#android:color/black" />
</RelativeLayout>
<LinearLayout
android:id="#+id/constraint_layout_conditions_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:gravity="center_vertical"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/book_condition_placeholder"
android:textColor="#color/colorPrimaryDark" />
<ImageButton
android:id="#+id/image_button_conditions"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="#dimen/dp10"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_spinner" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear_layout_conditions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/text_view_new"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/_new"
android:textColor="#color/dark_gray_text_color" />
<TextView
android:id="#+id/text_view_normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/normal"
android:textColor="#color/dark_gray_text_color" />
<TextView
android:id="#+id/text_view_old"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/old"
android:textColor="#color/dark_gray_text_color" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/constraint_layout_languages_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linear_layout_2"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:gravity="center_vertical"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/language"
android:textColor="#color/colorPrimaryDark" />
<ImageButton
android:id="#+id/image_button_languages"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="#dimen/dp10"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_spinner" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_languages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Fragment Java code:
package org.kitapp.UI.Fragments.SharePostFragments.Fragment1;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import org.kitapp.R;
import org.kitapp.UI.Dialogs.ConditionDialog.ConditionOfBookDialog;
import org.kitapp.UI.Dialogs.LanguageDialog.LanguageDialog;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
public class OverViewFragment extends Fragment implements OverViewContractor.View {
private final String TAG = OverViewFragment.class.getSimpleName();
private Context mContext;
private OverViewCallback mListener;
private OverViewPresenter mPresenter;
#BindView(R.id.constraint_layout_conditions_root)
LinearLayout conditionRoot;
#BindView(R.id.constraint_layout_languages_root)
LinearLayout languagesRoot;
#BindView(R.id.linear_layout_conditions)
LinearLayout conditions;
#BindView(R.id.root_layout)
LinearLayout rootLayout;
#BindView(R.id.text_view_number_of_characters)
TextView numberOfChars;
#BindView(R.id.edit_text_name_of_book)
EditText nameOfBook;
public interface OverViewCallback {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_over_view, container, false);
ButterKnife.bind(this, view);
new OverViewPresenter(this);
//rootLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
return view;
}
////// CONTRACTOR METHODS //////
#Override
public void setPresenter(OverViewPresenter presenter) {
this.mPresenter = presenter;
}
////// LISTENERS //////
#Override
#OnClick(R.id.image_button_conditions)
public void showConds() {
ConditionOfBookDialog conditionOfBookDialog = new ConditionOfBookDialog(mContext);
conditionOfBookDialog.show();
}
#Override
#OnClick(R.id.image_button_languages)
public void showLangs() {
LanguageDialog languageDialog = new LanguageDialog(mContext);
languageDialog.show();
}
#Override
#SuppressLint("SetTextI18n")
#OnTextChanged(R.id.edit_text_name_of_book)
public void onNameOfBookChange() {
int len = nameOfBook.getText().toString().trim().length();
numberOfChars.setText(len + "/50");
if (len > 50) {
nameOfBook.setText(nameOfBook.getText().toString().substring(0, 50));
nameOfBook.setSelection(nameOfBook.getText().length());
}
}
////// FRAGMENT METHODS //////
#Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
if (context instanceof OverViewCallback) {
mListener = (OverViewCallback) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OverViewCallback");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
UI Design and Dialog
In the image above, there are EditTexts Name Of Book and Name of Author. Before showing a dialog, both of them work like a charm. When writing something, the cursor automatically scrolls to the end. Once Dialog has been shown and dismissed, none of them works properly, and the cursor stays at the end of the EditText but still the characters are being written continues in the hidden part.
In this UI it cannot be seen because of the issue, but I've written.
Roses are red, violets are blue, Stackoverflow I love u
But only Roses are red, violets are blue, Stac can be seen.
Weird buggy layout UI image
Please, help me to solve this problem. Thanks in advance.
As a solution, I don't know the underlying reason, but the problem solved once I removed Butterknife library from my project.

Error in StartActivity Android

my code below is crashing. I am trying to have an app where the user inputs a word in the EditText and, after clicking the button, the app checks if the inputed word equals "hello", and if it does, the app takes the user to another activity.
The error is:
java.lang.IllegalStateException: Could not find method toHi (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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.reynaldo.myapplication.MainActivity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/button"
android:onClick="toHi (MainActivity)" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="hi"
android:ems="10"
android:id="#+id/text"
android:layout_marginTop="15dp"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void toHi (View v){
EditText text = (EditText) findViewById(R.id.text);
String key = text.getText().toString();
if (key.equals("hello")){
Intent i = new Intent (this, Main2Activity.class);
startActivity(i);
}
}
}
The problem is with the value of onClick:
replace
android:onClick="toHi (MainActivity)"
with
android:onClick="toHi"
The problem is that your event specified in Button onClick is "toHi (MainActivity)" while you are defining method public void toHi(View view).
One thing more,there are circular dependencies in your xml file.You are specifiying layout_below in both button and edit text.
Code like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/button"
android:layout_below="#+id/text" //Specify this here only
android:onClick="toHi" /> // Change here
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="hi"
android:ems="10"
android:id="#+id/text"
android:layout_marginTop="15dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

Using the decimal EditText to change Currency ?

I am building a very simple currency converter app for a course I am doing.
The App has a decimal Edit text box where you enter the amount and when you press the button it will convert the amount from £ sterling to U.S dollars and gives the user the difference in a Toast message.
I need help with the java code for the currency conversion part , if anyone can help that will be much appreciated.
xml
<?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: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.hydraquip_10.myapplication.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#drawable/currency"
android:scaleType="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText"
android:layout_marginTop="134dp"
android:layout_below="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Result"
android:id="#+id/button"
android:layout_marginTop="35dp"
android:onClick="changeCurrency"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Enter amount £"
android:id="#+id/textView"
android:layout_alignBottom="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="56dp"
android:gravity="center" />
</RelativeLayout>
main java
package com.example.hydraquip_10.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeCurrency(View view){
EditText changeNumber = (EditText) findViewById(R.id.editText);
Toast.makeText(getApplicationContext(), "Currency = $" + changeNumber.getText().toString(),Toast.LENGTH_LONG).show();
}
}

Android ActionBar with custom layout does not occupy whole width of parent

This is my xml fil
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="fill_horizontal"
android:gravity="fill_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:background="#fff556ff"
android:id="#+id/actionbar_relativelayout"
>
<ImageView
android:clickable="true"
android:id="#+id/imgMenu"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:paddingLeft="10dp"
android:src="#drawable/menu_dashboard" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="22dp"
android:layout_toEndOf="#+id/imgMenu"
android:layout_toRightOf="#+id/imgMenu"
android:text="#string/app_name"
android:textColor="#ffffff"
android:textSize="14sp" />
<ImageView
android:id="#+id/imgUser"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:src="#drawable/user" />
<ImageView
android:id="#+id/imgMessage"
android:layout_width="40dp"
android:clickable="true"
android:layout_height="40dp"
android:layout_marginTop="15dp"
android:layout_toLeftOf="#+id/imgUser"
android:src="#drawable/message_dashboard"
/>
<ImageView
android:id="#+id/imgInfo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginRight="80dp"
android:layout_marginTop="15dp"
android:src="#drawable/ic_action_search"
android:clickable="true"/>
</RelativeLayout>
</LinearLayout>
This is my Activity
RelativeLayout custom_RelativeLayout = (RelativeLayout)findViewById(R.id.actionbar_relativelayout);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM );
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setBackgroundDrawable(
getApplicationContext().getResources().getDrawable(
R.drawable.drawable_actionbar_back));
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
I want to its occupy whole space of action bar but it does not occupy i also read many answers on stackoverflow but didn't work.
1) I want match parent space in action bar
Edit This is my main Avtivity
package com.example.qasim.cashmanagement;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class DashboardActivity extends ActionBarActivity implements View.OnClickListener {
View viewAction;
ImageView imgUserbutton;// Actionbar
ImageView imgMessagebutton;// Actionbar
ImageView imgInfobutton;// Action bar
ImageView imgMenubutton;// Actionbar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard_two);
android.support.v7.app.ActionBar actionbar = getSupportActionBar();
GetActionBarDetails();// Method for inflating ActionBar
/*
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);*/
/* ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
View actionBarView = LayoutInflater.from(this).inflate(R.layout.custom_actionbar, null);
actionBar.setCustomView(actionBarView, lp);*/
// actionBar.setCustomView(R.layout.custom_actionbar);
/* getSupportActionBar().setCustomView(R.layout.custom_actionbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setBackgroundDrawable(
getApplicationContext().getResources().getDrawable(
R.drawable.drawable_actionbar_back));
viewAction = getSupportActionBar().getCustomView();*/
}
#Override
protected void onResume() {
super.onResume();
}
private void GetActionBarDetails()
{
RelativeLayout custom_RelativeLayout = (RelativeLayout)findViewById(R.id.actionbar_relativelayout);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM );
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setBackgroundDrawable(
getApplicationContext().getResources().getDrawable(
R.drawable.drawable_actionbar_back));
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
imgUserbutton = (ImageView) findViewById(R.id.imgUser);
imgInfobutton = (ImageView) findViewById(R.id.imgInfo);
imgMessagebutton = (ImageView)findViewById(R.id.imgMessage);
imgMenubutton = (ImageView)findViewById(R.id.imgMenu);
imgMenubutton.setOnClickListener(this);
imgMessagebutton.setOnClickListener(this);
imgInfobutton.setOnClickListener(this);
imgUserbutton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
}
}
If you want to get the height of native action bar then use ?android:attr/actionBarSize in XML.
Now your XML will look like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#fff556ff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="#+id/imgMenu"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.4"
android:text="#string/app_name"
android:textColor="#ffffff"
android:textSize="20sp" />
<ImageView
android:id="#+id/imgUser"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imgMessage"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imgInfo"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</RelativeLayout>

Relative layout alignment in android

In the below posted code, i'm not able to see the text in single line and at a time i want only 6 text to be displayed, but i'm able to see morethan 20 text's. can you have a look once why it is..
i need my view to be like this, full view should be covered by 6 texts only 3 by 3
|afsafasfaf afasfaffasfas|
| |
|cascsdfs asdaddasdada|
| |
Mainactivity.java
package com.example.sampleoporj;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
Button mbutton;
MainActivity activity;
LinearLayout mEventsLayout;
HorizontalScrollView mHorizontalscroll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mbutton = (Button)findViewById(R.id.button1);
mEventsLayout = (LinearLayout)findViewById(R.id.eventslayout);
mbutton.setOnClickListener(btnlistener);
activity = this;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
OnClickListener btnlistener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0 ;i <10 ; i++)
{
LayoutInflater inflater = activity.getLayoutInflater();
View view = (View)inflater.inflate(R.layout.samplelayout,null);
mEventsLayout.addView(view);
}
}
};
}
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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/helloworld"
android:text="#string/hello_world" />
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/helloworld"
android:id="#+id/horizontalscroll">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/eventslayout"></LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="18dp"
android:layout_toLeftOf="#+id/helloworld"
android:text="Button" />
</RelativeLayout>
Samplelayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".45"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".10"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".45"
android:orientation="vertical" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
i request the output should look like this, i need a horizontal scroll & at a time i need only 3 + 3 text to display (in image i have showed only 2 + 2 ). As i notice horizontal scroll shrinks if i add linearlayout with "fill_parent" param.

Categories

Resources