I want to display instruction activity when user opens the app for the first time. I am using shared preference for that. Whatever I am doing so far is working. But I think my way of achieving this is not right.
What I am following:
I am drawing a transparent instruction image(with instructions) in photoshop.
Checking if user is opening that page for the first time(using shared preference).
Displaying that particular image in an activity with translucent theme
private void showFrontPageGuideIfFirstTime(){
if(!prefKeeper.getBoolean(PreferenceKey.FRONT_GUIDE)){
Intent intent = new Intent(this, ShowGuide.class);
intent.putExtra(BACKGROUND_KEY, R.drawable.front_page_png);
this.startActivity(intent);
prefKeeper.putBoolean(PreferenceKey.FRONT_GUIDE, true);
}
}
And my instruction page looks something like(made in photoshop):
The Instruction Image
But I think by this way it would not work in all smart phone screens.
Where am I wrong, and what would be the best way of doing this?
Implementation as below
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="#+id/framelayout"
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="in.excogitation.example_mvptdd.MainActivity">
<!-- Include your layout here-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</FrameLayout>
In your activity.java
public class MainActivity extends AppCompatActivity {
FrameLayout frameLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Parent FrameLayout
frameLayout = (FrameLayout) findViewById(R.id.framelayout);
// Dynamically create a relativelayout which will be appended to framelayout
final RelativeLayout relativeLayout = new RelativeLayout(getApplicationContext());
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
relativeLayout.setBackgroundColor(Color.DKGRAY);
relativeLayout.setAlpha(0.7f);
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// remove the whole relativelayout on click
frameLayout.removeView(relativeLayout);
}
});
RelativeLayout.LayoutParams params;
// Place 1st 30x40 ImageView at (50,60) coordinates
params = new RelativeLayout.LayoutParams(100, 100);
params.leftMargin = 20;
params.topMargin = 50;
final ImageView imageView1 = new ImageView(getApplicationContext());
imageView1.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_star));
// Add 1st imageview to relative layout
relativeLayout.addView(imageView1, params);
// Place 2nd 30x40 ImageView at (100,60) coordinates
params = new RelativeLayout.LayoutParams(120, 120);
params.leftMargin = 800;
params.topMargin = 450;
final ImageView imageView2 = new ImageView(getApplicationContext());
imageView2.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_star));
// Add 2nd imageview to relative layout
relativeLayout.addView(imageView2, params);
// finally add it ot the framelayout
frameLayout.addView(relativeLayout);
}
}
Ofcourse you should modify this code with your own images and colors and interactions. Its just a simple working version that is better than loading a whole image upfront when you all you want is smaller helper images on a translucent background for the instructions.
Also you in this way you make things more Android-ish and editable. You can add more children to the relative layout like a textview to include instructions.
Screenshot on load of app and hence the relative layout as an overlay.
Screenshot on click/touch , the relative layout is removed.
Related
I have made a customView for a .gif image support in android. I got success in that, but currently that view is aligned to the top left corner of my activity. I want it to be aligned to the center of my activity. Please tell me how can I do this.
My code is as below:
GifwebView view = new GifwebView(this, "file:///android_asset/p3.gif");
view.setBackgroundColor(Color.parseColor("#00000000"));
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rl_pics);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel.setBackgroundColor(Color.parseColor("#00000000"));
view.setLayoutParams(p);
rel.addView(view);
CustomView
public class GifwebView extends WebView {
public GifwebView(Context context, String path) {
super(context);
// TODO Auto-generated constructor stub
loadUrl(path);
}
}
Your activity main layout (RelativeLayout), it's height and width should be match_parent, so it will take the whole screen, and it's gravity should be set to center, so that the elements in it will be positioned to center of the screen.
XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_pics"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</RelativeLayout>
Activity:
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rl_pics);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
GifwebView view = new GifwebView(this, "file:///android_asset/p3.gif");
view.setBackgroundColor(Color.parseColor("#00000000"));
view.setLayoutParams(p);
rel.addView(view);
p.addRule(RelativeLayout.CENTER_HORIZONTAL);
view.setGravity(Gravity.CENTER_VERTICAL);
This should work. Also if you want the activity to take the whole screen instead of the requiered for filling your content you should use match_parent instead of wrap_content.
edit: if your custom view doesn't implement the setGravity(int) method, you can do this instead:
p.addRule(RelativeLayout.CENTER_HORIZONTAL|RelativeLayout.CENTER_IN_PARENT, view.getId());
I'm having a difficulty adding buttons dynamically to a ScrollView. The code below is adding the buttons BUT there is no scroller.
If I'm putting the buttons directly in the XML (not dynamically) it's working and I can scroll down/up.
My view:
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="264dp"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="264dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
>
** HERE THE BUTTONS SHOULD BE ADDED DYNAMICALLY **
</LinearLayout>
</ScrollView>
The code which adding buttons:
// create new button
final Button newbutton = new Button(this);
// set background color
newbutton.setBackgroundColor(Color.GRAY);
// set width and height
newbutton.setWidth(50);
newbutton.setHeight(20);
// set position
newbutton.setY(((float)numOfButton*20)+20);
newbutton.setX(100);
// set text
newbutton.setText(Integer.toString(numOfButton));
// create patameter
final LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
//set listener
android.view.View.OnClickListener buttonListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// make all the DrawView invisible
for(View view : comments){
view.setVisibility(View.INVISIBLE);
}
// set the chosen comment visible
comments.get(numOfButton).setVisibility(View.VISIBLE);
boardsHandler.setCurrenBoard(numOfButton);
}};
newbutton.setOnClickListener(buttonListener);
// creating a thread to add button
buttons.post(new Runnable() {
#Override
public void run() {
buttons.addView(newbutton, p);
}
});
Is it something with the LinearLayout.LayoutParams p ?
Thanks!
Try following code
first do
LinearLayout myContainer = findViewById(R.id.layoutId);
When you set parameters for a view, they need to correspond to the parent view for your widget.
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT);
finally add button as you are doing.
try and tell if it works
Setting X and Y position will not work. The LinearLayout layouts it's children vertically or horizontally, only taking their width/height into account.
Besides this -- have you tried calling buttons.invalidate() after buttons.addView(...). This should refresh the layout and should show your newbutton.
This is a rather old post but I found it quickly when doing research on that kind of problem. So I'll post am answer anyway, maybe it'll be of help to anyone..
I had a similar problem with a relative layout to which buttons were added dynamically. I found a workaround in defining the layout's size manually when adding the buttons. For your case, adding the line
buttons.getLayoutParams().height = numOfButton*20+40;
after
buttons.addView(newbutton, p);
might help, though it's probably not the best solution.
I thought my mistake was using the RelativeLayout at all, but since you appear to have the same problem...
Ever thought of using a table layout?
I am using the following layout and want to add textviews one below another dynamically(depending on the data I want to display)
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/contain"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</ScrollView>
So I tried this
public class Feeds extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent=getIntent();
Bundle b=intent.getExtras();
String s= b.getString("datapack");
Log.w("String",s);
String data[]=s.split("#");
String temp="";
LinearLayout l=(LinearLayout)findViewById(R.id.contain);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
(LayoutParams.WRAP_CONTENT), (LayoutParams.WRAP_CONTENT));
RelativeLayout relative = new RelativeLayout(getApplicationContext());
relative.setLayoutParams(lp);
for(int i=1;i<data.length;i++)
{Log.w("i data",data[i]);
temp=temp + data[i] + ",";
Log.w("tag",temp);
if(i%5==0)
{Log.w("data",temp);
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(lp);
tv.setId(i);
tv.setText(temp);
relative.addView(tv, lp);
temp=null;
}
}
l.addView(relative);
}
}
I am using Scrollview for the first time so that is crating a bit of problem but the main problem is that the textviews in the relative layout overlap one another.How can I specify the gap between each view??
If you're adding views into a RelativeLayout you have to set where the view will be placed, different than LinearLayout that you only need to add the views inside there.
Trye to change to this
LinearLayout layout= new LinearLayout (getApplicationContext());
layout.setLayoutParams(lp);
And here you go a little tip. Do not do this
if(i%5==0)
{Log.w("data",temp);
try to do this
if(i%5==0){
Log.w("data",temp);
it's better to read and understand
I don't see where you are defining the positions for each textview, ie layout_below, layout_toLeftOf... If no positions are defined the relative layout stacks each view in the upper left corner.
Addition
It seems digulino and I agree. I would like to add that you should be able to get the layout scheme you want with only a RelativeLayout inside a ScrollView, maybe a HorizontalScrollView too if you want horizontal scrolling with fling gesture support.
Since I'm still just learning Android (and it appears Amazon says it'll be 2 months till I get the Hello, Android book) I'm still playing around with doing simple things. I have no problem getting an icon to display with the click of a button on my RelativeLayout using ImageView. The code for creating it is as follows:
private int mIconIdCounter = 1;
private ImageView addIcon(){
ImageView item = new ImageView(this);
item.setImageResource( R.drawable.tiles );
item.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
if( mIconIdCounter != 1 ){
params.addRule(RelativeLayout.RIGHT_OF, 1 );
}
item.setLayoutParams( params );
item.setId( mIconIdCounter );
++m_IconIdCounter;
return item;
}
and the code to add the item is:
Button addButton = (Button)findViewById(R.id.add_new);
addButton.setOnClickListener( new OnClickListener(){
#Override
public void onClick(View view) {
addContentView( addIcon(), new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ) );
}
});
When I click my button what happens is all the newly created views are placed atop one another. I'd like them to be placed to the right of the next element. I did a quick search on SO for articles relating to RelativeLayout and found some that were similar (here, here, here, and here) but while these addressed getting the content into the RelativeView they didn't seem to address the positioning aspect.
What am I doing wrong?
EDIT:
My main xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/add_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_new"
android:layout_alignParentBottom="true" />
</RelativeLayout>
It looks like you might be adding the view to the root of the layout xml instead of the RelativeLayout.
You could try:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.my_layout);
layout.addView(addIcon());
you are creating new relative layout inside function call. So every time new relative layout created and it added in the view when click button . Use common relative layout.
I created layout through code so that i can add multiple views on that layout using code below.
public class AndroidPrepChart extends Activity {
GraphicalView gView;
GraphicalView gView2;
GraphicalView gView3;
BarChart barChart = new BarChart();
BarChart2 barChart2 = new BarChart2();
BarChart3 barChart3 = new BarChart3();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gView = barChart.execute2(this);
gView2 = barChart2.execute2(this);
gView3 = barChart3.execute2(this);
LinearLayout layout = new LinearLayout(this);
layout.addView(gView, 150, 200);
layout.addView(gView2, 150, 200);
layout.addView(gView3, 150, 150);
setContentView(layout);
}
}
Here output screen contains three charts but i want to position third chart in the second line. Please help me to solve this problem. I am beginner in Android.
You can achieve this by nesting multiple LinearLayouts and changing the orientation property
in XML this would look something like this (showing only the relevant elements and attributes):
<LinearLayout android:orientation="vertical">
<LinearLayout android:orientation="horizontal">
<!-- Your first 2 graphs go in this LinearLayour -->
</LinearLayout>
<LinearLayout android:orientation="horizontal">
<!-- The third graph goes in here -->
</LinearLayout>
</LinearLayout>
You can programmatically manipulate the orientation of the LinearLayout by using the setOrientation method. E.g:
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
Are you completely sure that you can't do it with an XML? Coding gui from the code is quite more difficult.
In relation to your question:
I guess you want the third GraphicalView to be to the right of the second one.
Two ways to do it:
Using a RelativeLayout or using a second LinearLayout.
Example:
LinearLayout layout = new LinearLayout(this);
layout.addView(gView, 150, 200);
LinearLayout two = new LinearLayout(this);
two.setOrientation(LinearLayout.VERTICAL);
two.addView(gView2, 150, 200);
two.addView(gView3, 150, 150);
layout.addView(two);
Try not using that way of giving size to a View, you should use setLayoutParams.
For instance your layout should have something like:
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));